Javascript 角度重复:构造新对象
varJavascript 角度重复:构造新对象,javascript,angularjs,Javascript,Angularjs,var$scope.notif保存此数据: 在弹出窗口中,我想查看不同的消息,因此我使用: <ion-list><ion-item ng-repeat="n in notif">{{n.msg}}</ion-item></ion-list> 但是$scope.notifnew现在是未定义的。您可以筛选出不喜欢的属性: var data = test.notification.android.payload; $scope.notif = Obj
$scope.notif
保存此数据:
在弹出窗口中,我想查看不同的消息,因此我使用:
<ion-list><ion-item ng-repeat="n in notif">{{n.msg}}</ion-item></ion-list>
但是
$scope.notifnew
现在是未定义的
。您可以筛选出不喜欢的属性:
var data = test.notification.android.payload;
$scope.notif = Object.keys(data).filter(function(key){
return /^\d+$/.test(key); // or !!data[key].msg
}).map(function(key){
return data[key];
});
我知道。
$scope.notif.length
不起作用,因为$scope.notif
是一个对象而不是数组。您可以使用以下方法进行迭代:
var messages = [];
angular.forEach($scope.notif, function(val, key){
if (!isNaN(parseInt(key))) {
messages.push(val);
}
})
但在我看来,这种结构似乎“破碎了”。此外,有效载荷应为阵列imo
var test = {
"user_ids":["...","..."],
"notification":
{
"alert":"dummy",
"android":{
"title":"New alert 2",
"payload":{
"msg":"lala",
"msg_title":"Thanks!",
"notif_type":"alert",
"0":{
"msg":"Message 1"
},
"1":{
"msg":"Message 2","msg_title":"Thanks!"
}
}
}
}
}
你能说得更清楚些吗?
$scope.notif
是一个对象而不是数组。@Sajeetharan我添加了一个代码笔示例你不想显示这3个项目吗?不,只显示“消息1”和“消息2”(如果有更多,则显示更多),但决不显示空行。
var test = {
"user_ids":["...","..."],
"notification":
{
"alert":"dummy",
"android":{
"title":"New alert 2",
"payload":{
"msg":"lala",
"msg_title":"Thanks!",
"notif_type":"alert",
"0":{
"msg":"Message 1"
},
"1":{
"msg":"Message 2","msg_title":"Thanks!"
}
}
}
}
}