1

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.

3
  • Hi, sorry for the confusion, I have updated the question. Commented Nov 6, 2017 at 19:14
  • Don't return in the loops. Default a variable to true and then set it to false if any elements are not found. Then at the end, return that variable. Commented Nov 6, 2017 at 19:16
  • Array.prototype.every() will prove useful for you here. Commented Nov 6, 2017 at 19:29

3 Answers 3

2

Use lodash's _.intersectionBy(), and compare the length of the result with the length of the shorter array:

var arr1 = [{"id":"id1","name":"name1","other":"other"},{"id":"id2","name":"name2","other":"other"},{"id":"id3","name":"name3","other":"other"}];
var arr2 = [{"id":"id1","name":"name1"},{"id":"id3","name":"name3"}];
var arr3 = [{"id":"id1","name":"name1"},{"id":"id5","name":"name3"}];

var result1_2 = _.intersectionBy(arr2, arr1, 'id').length === arr2.length;

var result1_3 = _.intersectionBy(arr3, arr1, 'id').length === arr3.length;

console.log(result1_2);

console.log(result1_3);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

1

If the object properties have the same order, this could solve your problem:

const array1 = [
  {
    "id": "id1",
    "name": "name1"
  },
  {
    "id": "id2",
    "name": "name2"
  }
],
array2 = [
  {
    "id": "id1",
    "name": "name1",
    "other": "other"
  },
  {
    "id": "id2",
    "name": "name2",
    "other": "other"
  },
  {
    "id": "id3",
    "name": "name3",
    "other": "other"
  }
];

let present = true,
item,
property,
properties;

for (let i = 0; i < array1.length && present; i++) {
    item = array1[i];
    properties = Object.keys(item);

  for (let j = 0; j < properties.length; j++) {
    property = item[properties[j]];

    if (item.hasOwnProperty(properties[j])) {
      if (property !== array2[i][Object.keys(array1[i])[j]]) {
        present = false;
      } 
    }
  }
}

Comments

0

very simple an ugly way is to turn your objects into strings and compare them but this requires your object to have the same properties

 var a1 = [
  {
    "id": "id1",
    "name": "name1"
  },
  {
    "id": "id3",
    "name": "name3"
  }
];

var a2 = [
  {
    "id": "id1",
    "name": "name1",
  },
  {
    "id": "id2",
    "name": "name2",
  },
  {
    "id": "id3",
    "name": "name3",
  }
];

var a3 =  [
  {
    "id": "id1",
    "name": "name1"
  },
  {
    "id": "id5",
    "name": "name3"
  }
];


var doesArrayBContainArrayA = function(a,b)
{
  var u = [];
  a.map(e => 
        {
          var match = false;
          b.forEach(function(bx){
            if(!match){
             match = JSON.stringify(bx) === JSON.stringify(e);  
            }
          });
          if (!match){ 
              //console.log(JSON.stringify(bx)+'--'+JSON.stringify(e));
              u.push(e);   //add non existing item to temp array
            }

  });
  return u.length === 0;
}
var result = doesArrayBContainArrayA(a3,a2);
var result2 = doesArrayBContainArrayA(a1,a2);
console.log(result);
console.log(result2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.