Reputation: 89
Trying to merge two arrays with some simmilar occurances in two array here's what i did
var json_dest = [
{
"legID":"12121",
"message":212112
},
{
"legID":"12122",
"message":212112
}
];
var json_src = [
{
"legID":"12121",
"message":212100
},
{
"legID":"12123",
"message":212112
}
];
console.log(angular.merge(json_dest, json_src));
the output is:
[
{
"legID":"12121",
"message":212100
},
{
"legID":"12123",
"message":212112
}
]
it merged the duplicates
but i am missing the other legID "12123"
i need to know how it can be done efficiently ?
and also why is it happening ?
Upvotes: 1
Views: 89
Reputation: 1718
syntax for merge is
var object = angular.merge({}, obj1, obj2);
it is showing only second element
try using
console.log(angular.merge(dst, src1, src2));
Upvotes: 0
Reputation: 388
angular.merge
is not used to merge arrays but to deeply extend the destination object with enumerable properties of the source object:
Deeply extends the destination object dst by copying own enumerable properties from the src object(s) to dst. You can specify multiple src objects. If you want to preserve original objects, you can do so by passing an empty object as the target:
var object = angular.merge({}, object1, object2)
Source
If you just want to combine the two arrays into one you don't need an angular API to do that. Simply use concat:
json_dest = json_dest.concat(json_src);
If you want to remove the duplicates by a certain property, for example legID in your case, you can do that after combining the arrays. There are plenty of resources on how to do that. See for example this question: Remove duplicates from an array of objects in javascript
Upvotes: 2