Reputation: 1040
I have two arrays like below:
var oldArray = [{ id: '10', name: 'ABC', type: 'type1' },
{ id: '11', name: 'XYZ', type: 'type1' }]
var newArray=[{ id: '10', name: 'ABC', type: 'type1' },
{ id: '11', name: 'XYZ', type: 'type1' }
{ id: '12', name: 'PQR', type: 'type1' },
{ id: '13', name: 'SomeNewData', type: 'type2' }]
I want to get that PQR
object from the newArray
because it is of type1
and the only object which is difference in the oldArray
and push that object into oldArray
.
I tried it using the difference
method of lodash but I couldn't get it done. Can I achieve this using lodash
? Any solution using native JavaScript will also be useful.
Any help would be much appreciated. Thanks!
Upvotes: 0
Views: 309
Reputation: 312
How with vanilla?
var oldArray = [{
id: '10',
name: 'ABC',
type: 'type1'
},
{
id: '11',
name: 'XYZ',
type: 'type1'
}
]
var newArray = [{
id: '10',
name: 'ABC',
type: 'type1'
},
{
id: '11',
name: 'XYZ',
type: 'type1'
}, {
id: '12',
name: 'PQR',
type: 'type1'
},
{
id: '13',
name: 'SomeNewData',
type: 'type2'
}
]
var existingArray = oldArray.map(arr => arr.id);
var filteredArray = newArray.filter(arr => arr.type === 'type1' && !existingArray.includes(arr.id));
var mergedArray = oldArray.concat(filteredArray);
console.log(mergedArray);
Upvotes: 1
Reputation: 48610
You can diff the two arrays by comparing the objects by equivalency. The basic isEq
function below was borrowed from here: "Object Equality in JavaScript".
Once you can compare two objects, just diff them by looping over both arrays and building a difference array by checking if the current item exists in the other list.
You do this two times, once for the first list and again for the second. Except on the second time around, you check if the item is already in the diff list.
Edit: If you only want to look at items where type = "type1"
, filter them out before you proceed.
var oldArray = [{
id: '10',
name: 'ABC',
type: 'type1'
}, {
id: '11',
name: 'XYZ',
type: 'type1'
}]
var newArray = [{
id: '10',
name: 'ABC',
type: 'type1'
}, {
id: '11',
name: 'XYZ',
type: 'type1'
}, {
id: '12',
name: 'PQR',
type: 'type1'
}, {
id: '13',
name: 'SomeNewData',
type: 'type2'
}]
console.log(diff(
oldArray.filter(item => item.type === 'type1'),
newArray.filter(item => item.type === 'type1')
));
function diff(arrA, arrB) {
let diff = [];
arrA.forEach(itemA => {
if (!arrB.some(itemB => isEq(itemA, itemB))) {
diff.push(itemA);
}
})
arrB.forEach(itemB => {
if (!diff.some(p => isEq(itemB, p)) && !arrA.some(itemA => isEq(itemA, itemB))) {
diff.push(itemB);
}
})
return diff;
}
function isEq(a, b) {
var aProps = Object.getOwnPropertyNames(a);
var bProps = Object.getOwnPropertyNames(b);
if (aProps.length != bProps.length) {
return false;
}
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
if (a[propName] !== b[propName]) {
return false;
}
}
return true;
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
Upvotes: 1
Reputation: 122047
You could find the difference using filter
and some
methods and also check the type.
var oldArray = [{ id: '10', name: 'ABC', type: 'type1' }, { id: '11', name: 'XYZ', type: 'type1' }]
var newArray=[{ id: '10', name: 'ABC', type: 'type1' }, { id: '11', name: 'XYZ', type: 'type1' }, { id: '12', name: 'PQR', type: 'type1' }, { id: '13', name: 'SomeNewData', type: 'type2' }]
var diff = newArray.filter(({ id, type }) => (
type == 'type1' && !oldArray.some(e => e.id == id)
))
console.log(diff)
Upvotes: 1