I have 2 array of object in that, I want to compare all of the first array object property and value present in the second array of object. (at the object level.)
array 1
[
{
"id": "id1",
"name": "name1"
},
{
"id": "id3",
"name": "name3"
}
]
array 2
[
{
"id": "id1",
"name": "name1",
"other": "other"
},
{
"id": "id2",
"name": "name2",
"other": "other"
},
{
"id": "id3",
"name": "name3",
"other": "other"
}
]
In this case it will return true because, all of array 1 values present in array 2.
but if I have one property value that does not match with array 2, that will return false.
[
{
"id": "id1",
"name": "name1"
},
{
"id": "id5",
"name": "name3"
}
]
This will return false because { "id": "id5","name": "name3"} does not present in the array2.
I'm trying to compare with two for loop but I wanted to return false only once after both loop completes comparing all properties and value.now,
here is the two loops I'm trying, I know this is wrong because, it will return false when there is no value find and it will not go into the further loop.
for (var i = 0, i < a.length; i++) {
for (var j = 0, j < b.length; j++) {
if (b[j].id === a[i].id && b[j].name === a[i].name) {
console.log('all values found');
return true;
}
console.log('some values does not found');
return false;
}
}
I'm also using lodash.
Array.prototype.every()will prove useful for you here.