In my Angularjs application, I have the following json :
$scope.listA =
[
{
"bId":777,
"cId":4,
"ctNm":"Software"
},
{
"bId":777,
"cId":2,
"ctNm":"Hardware"
},
{
"bId":777,
"cId":1,
"ctNm":"Malware"
}
]
And I need to convert this array in such a way that each json object of the array should contain items array of objects.
The logic to populate this items array of objects for each of this $scope.listA json object is to go and check in $scope.listB array of json objects, if that particular cId exist. If yes, keep all those json objects of $scope.listB under items array of each object in $scope.listA.
$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"
}
]
So the finally modified $scope.listA, should be as shown below :
[
{
"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"
}
]
}
]
The solution in either under score or in angular js util functions, both are fine for me. I prefer not to use for loop any other way is fine, foreach, map, etc