在我的 Angularjs 应用程序中,我有以下 json :
$scope.listA =
[
{
"bId":777,
"cId":4,
"ctNm":"Software"
},
{
"bId":777,
"cId":2,
"ctNm":"Hardware"
},
{
"bId":777,
"cId":1,
"ctNm":"Malware"
}
]
我需要以这样的方式转换这个数组:数组的每个 json 对象都应该包含 items
对象数组。
为每个 $scope.listA
json 对象填充此 items
对象数组的逻辑是去检查 $scope.listB
json 对象数组(如果特定的 cId
存在)。如果是,则将 $scope.listB
的所有 json 对象保留在 $scope.listA
中每个对象的 items
数组下。
$scope.listB =
[
{
"bId":0,
"cId":2,
"clsxId":24,
"ctNm":"Hardware",
"clNm":"Out 1"
},
{
"bId":0,
"cId":1,
"clsxId":99,
"ctNm":"Malware",
"clNm":"Srv"
},
{
"bId":0,
"cId":2,
"clsxId":26,
"ctNm":"Hardware",
"clNm":"Buss"
},
{
"bId":0,
"cId":2,
"clsxId":67,
"ctNm":"Hardware",
"clNm":"Pait"
}
]
所以最终修改后的$scope.listA
,应该如下所示:
[
{
"bId":777,
"cId":4,
"ctNm":"Software",
"items":[
]
},
{
"bId":777,
"cId":2,
"ctNm":"Hardware",
"items":[
{
"bId":0,
"cId":2,
"clsxId":24,
"ctNm":"Hardware",
"clNm":"Out 1"
},
{
"bId":0,
"cId":2,
"clsxId":26,
"ctNm":"Hardware",
"clNm":"Buss"
},
{
"bId":0,
"cId":2,
"clsxId":67,
"ctNm":"Hardware",
"clNm":"Pait"
}
]
},
{
"bId":777,
"cId":1,
"ctNm":"Malware",
"items":[
{
"bId":0,
"cId":1,
"clsxId":99,
"ctNm":"Malware",
"clNm":"Srv"
}
]
}
]
无论是 under Score 还是 Angular JS util 函数中的解决方案,都对我来说很好。我不喜欢使用 for 循环,任何其他方式都可以,foreach、map 等
最佳答案
尝试这样的事情:
for (var a = 0; a < $scope.listA.length; a++) {
var itemInA = $scope.listA[a];
itemInA.items = [];
for (var b = 0; b < $scope.listB.length; b++) {
var itemInB = $scope.listB[b];
if (itemInA.cId === itemInB.cId) {
itemInA.items.push(itemInB);
}
}
}
这将循环遍历 $scope.listA
中的每个项目,并向其中添加一个 items
数组。然后,它将循环遍历 $scope.listB
中的每个项目,并且如果 $scope.listB
中的项目与当前项目具有相同的 cId
在 $scope.listA
中,然后它将其推送到 $scope.listA
中当前项目的 items
数组中。
请参阅此工作示例,它将把生成的 listA 打印到控制台日志中。
var listA = [{
"bId": 777,
"cId": 4,
"ctNm": "Software"
}, {
"bId": 777,
"cId": 2,
"ctNm": "Hardware"
}, {
"bId": 777,
"cId": 1,
"ctNm": "Malware"
}];
var listB = [{
"bId": 0,
"cId": 2,
"clsxId": 24,
"ctNm": "Hardware",
"clNm": "Out 1"
}, {
"bId": 0,
"cId": 1,
"clsxId": 99,
"ctNm": "Malware",
"clNm": "Srv"
}, {
"bId": 0,
"cId": 2,
"clsxId": 26,
"ctNm": "Hardware",
"clNm": "Buss"
}, {
"bId": 0,
"cId": 2,
"clsxId": 67,
"ctNm": "Hardware",
"clNm": "Pait"
}];
for (var a = 0; a < listA.length; a++) {
var itemInA = listA[a];
itemInA.items = [];
for (var b = 0; b < listB.length; b++) {
var itemInB = listB[b];
if (itemInA.cId === itemInB.cId) {
itemInA.items.push(itemInB);
}
}
}
console.log(JSON.stringify(listA, null, 2));
输出:
[
{
"bId": 777,
"cId": 4,
"ctNm": "Software",
"items": []
},
{
"bId": 777,
"cId": 2,
"ctNm": "Hardware",
"items": [
{
"bId": 0,
"cId": 2,
"clsxId": 24,
"ctNm": "Hardware",
"clNm": "Out 1"
},
{
"bId": 0,
"cId": 2,
"clsxId": 26,
"ctNm": "Hardware",
"clNm": "Buss"
},
{
"bId": 0,
"cId": 2,
"clsxId": 67,
"ctNm": "Hardware",
"clNm": "Pait"
}
]
},
{
"bId": 777,
"cId": 1,
"ctNm": "Malware",
"items": [
{
"bId": 0,
"cId": 1,
"clsxId": 99,
"ctNm": "Malware",
"clNm": "Srv"
}
]
}
]
关于javascript - 需要从 Angular js 应用程序中的另一个 json 对象数组填充 json 数组的每个对象的 json 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33524095/