需要从 angular js 应用程序中的另一个 json 对象数组为 json 数组的每个对象填充 json 对象数组
need to populate json array of objects for each object of json array from another json array of objects in angular js application
在我的 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
对象数组的逻辑是去检查 [=44= 的 $scope.listB
数组] 对象,如果那个特定的 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
的项目与 $scope.listA
中的当前项目具有相同的 cId
,那么它将把它推到 items
$scope.listA
.
中当前项目的数组
查看此工作示例,它将结果列表 A 打印到控制台日志。
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"
}
]
}
]
你可以试试这个,
$scope.listA.forEach(function(aItem){
aItem.Items =[];
aItem.Items.addRange($scope.listB.where(function(bItem){return aItem.cId == bItem.cId;}));
});
在这里,我使用了一些扩展,我为 javascript 数组创建的
Object.defineProperty(Array.prototype, "addRange", {
value: function (values) {
for (var i = 0; i < values.length; i++) {
this.push(values[i]);
}
return this;
}, enumerable: false
});
Object.defineProperty(Array.prototype, "where", {
value: function (Predicate) {
var arr = [];
for (var i = 0; i < this.length; i++) {
if (Predicate(this[i])) arr.push(this[i]);
}
return arr;
}, enumerable: false
});
对于c#开发者来说有点友好,像lambda表达式一样使用它
在我的 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
对象数组的逻辑是去检查 [=44= 的 $scope.listB
数组] 对象,如果那个特定的 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
的项目与 $scope.listA
中的当前项目具有相同的 cId
,那么它将把它推到 items
$scope.listA
.
查看此工作示例,它将结果列表 A 打印到控制台日志。
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"
}
]
}
]
你可以试试这个,
$scope.listA.forEach(function(aItem){
aItem.Items =[];
aItem.Items.addRange($scope.listB.where(function(bItem){return aItem.cId == bItem.cId;}));
});
在这里,我使用了一些扩展,我为 javascript 数组创建的
Object.defineProperty(Array.prototype, "addRange", {
value: function (values) {
for (var i = 0; i < values.length; i++) {
this.push(values[i]);
}
return this;
}, enumerable: false
});
Object.defineProperty(Array.prototype, "where", {
value: function (Predicate) {
var arr = [];
for (var i = 0; i < this.length; i++) {
if (Predicate(this[i])) arr.push(this[i]);
}
return arr;
}, enumerable: false
});
对于c#开发者来说有点友好,像lambda表达式一样使用它