gpt4 book ai didi

javascript - 如何将我的子对象推送到数组?

转载 作者:行者123 更新时间:2023-12-03 04:48:30 25 4
gpt4 key购买 nike

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

enter image description here

我可以在我的 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);
})
})

但是输出是这样的: enter image description here

换句话说,我无法显示我的 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>

最佳答案

尝试使用:

 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 - 如何将我的子对象推送到数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42762248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com