Below is my json stored in :
$scope.regions = [];
{
"id": 100,
"regions": [
{
"id": 10,
"name": "Abc",
"rank": 0,
},
{
"id": 20,
"name": "Pqr",
"rank": 1,
},
{
"id": 30,
"name": "Lmn",
"rank": 2,
},
{
"id": 40,
"name": "xyz",
"rank": 3,
},
{
"id": 50,
"name": "GGG",
"rank": 4,
},
{
"id": 60,
"name": "YYY",
"rank": 5,
}
]
}
This is my another json stored in :
$scope.regionList = [];
var highestOrder = 3;
"regions": [
{
"id": 40,
"name": "xyz",
"rank": 0,
},
{
"id": 50,
"name": "GGG",
"rank": 1,
},
{
"id": 60,
"name": "YYY",
"rank": 2,
}
Now I want to merge $scope.regionList in to $scope.regions but for those records which are matching in both $scope.regionList and $scope.regions I would like to replace records of $scope.regions with $scope.regionList (only common records from both list).
And first non matching records from $scope.regionList will have order start using highestOrder and will keep incrementing for each non-matching records so final output will be like below :
Expected output :
"regions": [
{
"id": 10,
"name": "Abc",
"rank": 3,
},
{
"id": 20,
"name": "Pqr",
"rank": 4,
},
{
"id": 30,
"name": "Lmn",
"rank": 5,
},
{
"id": 40,
"name": "xyz",
"rank": 0,
},
{
"id": 50,
"name": "GGG",
"rank": 1,
},
{
"id": 60,
"name": "YYY",
"rank": 2,
}
As Abc is is the first non matching record so it will have order 3 and rest other will have order no from 3 ie 4,5 6, etc.
My code:
var highestOrder = 3;
var found = false;
for (var i = 0; i < $scope.regions.length; i++) {
if ($scope.regions[i].id == 100) {
found = true;
for (var j = 0; j < $scope.regionList.length; j++) {
for (var k = 0; k < $scope.regions[i].regions.length; k++) {
if ($scope.regions[i].regions[k].id == $scope.regionList[j].id) {
$scope.regions[i].regions[k].rank = $scope.regionList[j].rank;
}
else {
$scope.regions[i].regions[k].rank = highestOrder;
highestOrder = highestOrder + 1;
}
}
}
}
if (found)
break;
}
var regions = { "id": 100, "regions": [{ "id": 10, "name": "Abc", "rank": 0, }, { "id": 20, "name": "Pqr", "rank": 1, }, { "id": 30, "name": "Lmn", "rank": 2, }, { "id": 40, "name": "xyz", "rank": 3, }, { "id": 50, "name": "GGG", "rank": 4, }, { "id": 60, "name": "YYY", "rank": 5, } ] } var highestOrder = 3; var found = false; var regionList = [{ "id": 40, "name": "xyz", "rank": 0, }, { "id": 50, "name": "GGG", "rank": 1, }, { "id": 60, "name": "YYY", "rank": 2 } ] for (var i = 0; i < regions.length; i++) { if (regions[i].id == 100) { found = true; for (var j = 0; j < regionList.length; j++) { for (var k = 0; k < regions[i].regions.length; k++) { if (regions[i].regions[k].id == regionList[j].id) { regions[i].regions[k].rank = regionList[j].rank; } else { regions[i].regions[k].rank = highestOrder; highestOrder = highestOrder + 1; } } } } if (found) break; } console.log(regions)
You could use a hash table and build it with the elements of the array for updating.
Then iterate regions
and update rank
with either the hash's rank
or with highestOrder
. Increment highestOrder
after assigning.
var $scope = { regions: [{ id: 100, regions: [{ id: 10, name: "Abc", rank: 0, }, { id: 20, name: "Pqr", rank: 1, }, { id: 30, name: "Lmn", rank: 2, }, { id: 40, name: "xyz", rank: 3, }, { id: 50, name: "GGG", rank: 4, }, { id: 60, name: "YYY", rank: 5, }] }] }, regionsUpdate = [{ id: 40, name: "xyz", rank: 0, }, { id: 50, name: "GGG", rank: 1, }, { id: 60, name: "YYY", rank: 2, }], regionsId = 100, highestOrder = 3, hash = Object.create(null); regionsUpdate.forEach(function (a) { hash[a.id] = a; }); $scope.regions.some(function (a) { if (a.id === regionsId) { a.regions.forEach(function (b) { b.rank = hash[b.id] ? hash[b.id].rank : highestOrder++; }); return true; } }); console.log($scope);
.as-console-wrapper { max-height: 100% !important; top: 0; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.