我有3套Json数据
data1 [{id:1, systemName: "foo", moreInfo: "blah},
{id:2, systemName: "boo", moreInfo: "blahBlah}]
data2 [{systemName: "foo",count: 21},
{systemName: "boo",count: 454}]
data3 [{systemName: "foo",count: 643},
{systemName: "boo",count: 674}]
我需要的是从数据2和3中获取计数并将它们添加到data1中各自的对象,使用新名称,如data2Count和Data3Count
所以data1看起来像这样:
data1 [{id:1, systemName: "foo", moreInfo: "blah, data2Count: 21, data3Count: 643},
{id:2, systemName: "boo", moreInfo: "blahBlah, data2Count: 454, data3Count: 674}]
目前我只能做到这一点:
data1 [{id:1, systemName: "foo", moreInfo: "blah"}
{id:2, systemName: "boo", moreInfo: "blahBlah"} data2Count: 454]
使用:
angular.forEach(data1, function(value1, key1) {
angular.forEach(data2, function(value0, key0) {
if (value1.systemName === value1.systemName) {
$scope.sysFlow.data2Count = value1.count
}
})
})
答案 0 :(得分:1)
您可以使用Array#map
执行以下操作:
var data1 = [
{id:1, systemName: "foo", moreInfo: "blah"},
{id:2, systemName: "boo", moreInfo: "blahBlah"}
]
var data2 = [
{systemName: "foo",count: 21},
{systemName: "boo",count: 454}
]
var data3 = [
{systemName: "foo",count: 643},
{systemName: "boo",count: 674}
]
data1 = data1.map(function(obj) {
obj.data2Count = data2.filter(function(o) {
return o.systemName === obj.systemName
})[0].count;
obj.data3Count = data3.filter(function(o) {
return o.systemName === obj.systemName
})[0].count;
return obj;
})
console.log(data1)