Reputation: 5192
Is it possible to compare 2 object arrays in javascript. My requirement is i have to compare 2 object arrays and i have to take the common objects between those 2 object arrays:
Consider 1st object array:
[ { id_0: 356,
name_0: 'xxxxx',
id_1: 33,
name_1: 'yyyyyy',
id_2: 602,
name_2: 'zzzzzzz',
collecteddate: '31/03/2011',
key: 'kkkkkkk',
value: '0.41' },
{ id_0: 356,
name_0: 'xxxxxx',
id_1: 33,
name_1: 'yyyyyy',
id_2: 602,
name_2: 'zzzzzz',
collecteddate: '31/03/2011',
key: 'k1k1k1k1k1',
value: '0.20' },
{ id_0: 356,
name_0: 'xxxxx',
id_1: 33,
name_1: 'yyyyy',
id_2: 602,
name_2: 'zzzzzz',
collecteddate: '31/03/2011',
key: 'k2k2k2k2k2k2k2',
value: '1.30' }}]
My 2nd object array:
[ { id_0: 356,
name_0: 'xxxxx',
id_1: 33,
name_1: 'yyyyyy',
id_2: 602,
name_2: 'zzzzzzz',
collecteddate: '31/03/2011',
key: 'kkkkkkk',
value: '0.41' },
{ id_0: 356,
name_0: 'xxxxxx',
id_1: 33,
name_1: 'yyyyyy',
id_2: 602,
name_2: 'zzzzzz',
collecteddate: '31/03/2011',
key: 'k1k1k1k1k1',
value: '0.20' },
{ id_0: 356,
name_0: 'xxxxx',
id_1: 33,
name_1: 'yyyyy',
id_2: 602,
name_2: 'zzzzzz',
collecteddate: '31/03/2011',
key: 'k2k2k2k2k2k2k2',
value: '1.30' }}]
I have to compare the above 2 objects and i have to build another object with common entries of the above 2. In the above example every entry is same in 2 arrays so the third array should contain 3 entries. Is it possible to do this..Help me to solve this..Thanks in advance....
EDIT:
I have used _(underscore) library to check whether 2 objects are equal if equal i pushed that object into new array..Whether its good solution or not..
Upvotes: 2
Views: 61
Reputation: 239473
Note: This solutions assumes that all the values of the objects will be either strings or numbers.
function getCommonObjects(array1, array2) {
var result = [];
for (var i = 0; i < array1.length; i += 1) {
for (var j = 0; j < array2.length; j += 1) {
if (Object.keys(array1[i]).length === Object.keys(array2[j]).length){
var flag = true;
for (var key in array1[i]) {
if (array1[i][key] !== array2[j][key]) {
flag = false;
break;
}
}
if (flag) {
result.push(array2[j]);
break;
}
}
}
}
return result;
}
Upvotes: 1
Reputation: 4882
Solution without third library - pure javascript
Something like this (recursive) - could be optimized but less readable.
var compareAndMerge = function(object1, object2) {
var outputObject = {};
var compareSubObject = function(object1, object2) {
var outputObject = {};
for (var k in object1) {
for (var k2 in object2) {
if (typeof object1[k] === 'object' && typeof object2[k2] === 'object') {
var sameObject = compareSubObject(object1[k], object2[2]);
if (sameObject === false) {
return false;
}
outputObject[k] = object1[k];
} else if (object1[k] === object2[k]) {
outputObject[k] = object1[k];
} else {
return false;
}
}
}
return outputObject;
};
for (var k in object1) {
for (var k2 in object2) {
if (typeof object1[k] !== 'object' && object1[k] === object2[k]) {
outputObject[k] = object1[k];
} else if (typeof object1[k] === 'object') {
var sameObject = compareSubObject(object1[k], object2[k]);
if (sameObject !== false) {
outputObject[k] = sameObject;
}
}
}
}
return outputObject;
};
Upvotes: 1