【问题标题】:How to push my sub-objects to an array?如何将我的子对象推送到数组?
【发布时间】:2026-01-04 07:55:01
【问题描述】:

我正在从我的 Firebase 中检索数据,当然我得到的是对象:

我可以在我的 Ng-Repeat 中轻松显示它们,这不是问题,但是如果我想使用输入搜索字段和简单过滤器搜索它,它会返回错误,因为它没有接收一个数组但对象。

我决定将它们发送到这样的数组中:

var userId = firebase.auth().currentUser.uid;
var ref = firebase.database().ref('/accounts/' + userId + "/cards/");

var cards2 = [];
ref.orderByChild('cards').on("value", function(snapshot1) {
  var cards1 = snapshot1.val();
  cards2.push(cards1);
  console.log(snapshot1.val());
  $timeout(function(){
  $scope.cards = cards2;
  console.log(cards2);
  })
})

但是输出是这样的:

换句话说,我无法显示我的 ng-repeat,因为这两个对象作为子对象被推送到我的对象数组中!

如何对它们进行单独排序,以便在我的 ng-repeat 中显示它们并能够像这样过滤它们:

ng-repeat="card in cards | filter:searchText"

编辑:这是我的 HTML 代码:

<ion-view view-title="MY CARDS">
    <ion-nav-buttons side="right">
        <button class="button button-icon icon ion-android-more-vertical"></button>
    </ion-nav-buttons>
    <ion-content class="padding">
     <div class="list list-inset input-search">
            <label class="item item-input">
                <i class="icon ion-search placeholder-icon"></i>

                <input ng-model="searchText" type="text" placeholder="Search">
          </label>
        </div>
        <a><div  ng-click="toUserView($index)" value="Taba" ng-repeat="card in cards | filter:searchText" class="list card ">
            <div class="item item-avatar item-icon-right">
                <img ng-src="{{card.photoURL}}">
                <h2>{{card.name}}</h2>

                <!-- *class time / time and icon -->
                                <!-- *class icon-p / icon location -->
                <p class="icon-p">

                    {{card.description}}
                </p>
                <p style="color:white;" id="rotaryId{{$index}}" >{{card.rotaryId}}</p>
            </div>


        </div></a>

    </ion-content>
</ion-view>

【问题讨论】:

  • 你能分享你的html代码吗?你在哪里使用ng-repeat
  • 好的,我已经更新了

标签: javascript angularjs arrays firebase


【解决方案1】:
  1. Firebase 实际上并不存储数组,如果可以的话,最好避免在 Firebase 中使用数组:https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html

  2. 为什么不迭代对象的键? How can I iterate over the keys, value in ng-repeat in angular

变化:

ng-repeat="card in cards

到:

ng-repeat="(key, card) in cards

例如:

<a><div  ng-click="toUserView($index)" value="Taba" ng-repeat="(key, card) in cards | filter:searchText" class="list card ">
    <div class="item item-avatar item-icon-right">
        <img ng-src="{{card.photoURL}}">
        <h2>{{card.name}}</h2>

        <!-- *class time / time and icon -->
                        <!-- *class icon-p / icon location -->
        <p class="icon-p">

            {{card.description}}
        </p>
        <p style="color:white;" id="rotaryId{{$index}}" >{{card.rotaryId}}</p>
    </div>


</div></a>

【讨论】:

    【解决方案2】:

    尝试使用:

     var userId = firebase.auth().currentUser.uid;
     var ref = firebase.database().ref('/accounts/' + userId + "/cards/");
    
     var cards2 = [];
      ref.orderByChild('cards').on("value", function(snapshot1) {
           var cards1 = snapshot1.val();
           for(var i in cards1){
              cards2.push(cards1[i])
            }
            $scope.cards = cards2;
            console.log(cards2);
          })
        })
    

    在这种情况下,您将获得对象数组

    【讨论】:

    • 你好,谢谢你,添加这个结果还是一样的,你知道吗?
    • 你介意做一个完整的代码吗?我是javascript新手,你的回答会给我带来比我已经拥有的更多的麻烦! :p 提前谢谢你
    • 谢谢,关于我的代码,我应该用这个替换它的哪个部分?
    • 再次编辑@FrenchyNYC
    • 谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢
    最近更新 更多