identify difference objects in arrays

26 views
Skip to first unread message

udhay prakash pethakamsetty

unread,
Dec 12, 2017, 3:59:03 AM12/12/17
to nodejs
Hi all, 

I am new to nodejs. 

I have two arrays of objects. 

array1 = [
{
id: '1',       // unique key
name: 'apple'
},
{
id: '2',       // unique key  
name: 'mango'
}
];

array2 = [
{
id: '1',       // unique key
name: 'apple'
},
{
id: '3',       // unique key
name: 'car'
}
];

I want to get the difference objects. 
i.e., 
array1 - array2  =---> result 

{
id: '2',       // unique key  
name: 'mango'
}
Similarly, 
array2 - array1  =---> result 

{
id: '3',       // unique key  
name: 'car'
}

Nicholas Pedroso

unread,
Dec 12, 2017, 11:17:23 PM12/12/17
to nod...@googlegroups.com
Maybe there is a lot of ways to do it

Something like that should work

(function () {
'use strict'

var objA = { name: "John", id: 3, age: 42 },
objB = { name: "Lucas", id: 3, age: 31}

var getDiff = function (objA, objB) {
var result = {};

if (!objA || !objB) {
console.log('You need to provide two objects');
return false;
};


for (var key in objA) {
console.log('Initializing key ' + key);
// iterate over objA's properties
if (objA.hasOwnProperty(key)) {
console.log('Object has own property: ', key)
var objATempValue = objA[key];
console.log('Key ' + key +' value is ', objATempValue)
if (objB.hasOwnProperty(key)) {
console.log('objB has key ' + key + ' with a value of ' + objB[key])
if (objB[key] !== objATempValue) {
console.log(objB[key] + ' is different from ' + objATempValue)
result[key] = objATempValue;
console.log('Our result is now...', result)
}
}else {
console.log(objB[key] + ' is equal from ' + objATempValue)
result[key] = objATempValue;
}
};
};

return result;
};

console.log(getDiff(objA, objB));

})();

Um abraço,

Nicholas C. Pedroso

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/0f785290-f6a4-4d99-9e26-093217d5e1f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tony Mobily

unread,
Dec 12, 2017, 11:17:23 PM12/12/17
to nodejs
Hi,

Comparing two objects in javascript is anything but straightforward.

The easiest solution is to use Underscore, which has _.isEqual(obj1, obj2);
(It's a "simple" solution only in theory... have a look at the implementation! http://underscorejs.org/docs/underscore.html#section-113

Hope this helps,

Merc.

Vishwadeep Kapoor

unread,
Dec 13, 2017, 10:48:43 AM12/13/17
to nodejs
Hi,
The best way to do such things is using the https://lodash.com/docs/4.17.4#differenceBy

_.differenceBy([{ 'x'2 }, { 'x'1 }], [{ 'x'1 }], 'x');
// => [{ 'x': 2 }]

where "x" can be any key or an identifier.
Reply all
Reply to author
Forward
0 new messages