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
2017-12-12 5:41 GMT-02:00 udhay prakash pethakamsetty <
[email protected]>:
> 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'
> }
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/nodejs/0f785290-f6a4-4d99-9e26-093217d5e1f0%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/0f785290-f6a4-4d99-9e26-093217d5e1f0%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/CAOodZimdJN-XAetsS3DQ-uXpBNTJjEJsuVjeJkFcbfM4z0TAhQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.