0

There is a similar question on StackOverflow here in this question we need to get the difference in the array.

But I just want true or false. I don't need the different array.

I tried it like this:

groups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}]

checkedgroups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}, {id: 3, name: "check 3"}]

My solution: _.differenceBy(groups, checkedGroups, 'id').length

vikas95prasad
  • 1,234
  • 1
  • 12
  • 37

6 Answers6

1

You can try different arrays and check the output. It uses a filter function. If the elements are not equal the array returned by filter is empty. Otherwise array of length of the original array is returned.

var a = ['a', 'b'];
var b = ['a', 'b'];

function w(a, b) {
  if (a.length != b.length)
    return false;
  else {
    var k = a.filter((e) => b[a.indexOf(e)] == e)
    if (k.length == 0)
      return false;
    else if (k.length == a.length)
      return true
  }
}
console.log(w(a, b))
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
ellipsis
  • 12,049
  • 2
  • 17
  • 33
0

You can get the arrays of id's to compare the string version of the arrays:

var groups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}]

var checkedgroups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}, {id: 3, name: "check 3"}]

function differenceBy(g, c, id){
  var id1 = JSON.stringify(g.map(i => i[id]));
  var id2 = JSON.stringify(c.map(i => i[id]));
  if(id1 == id2) return true;
  else return false;
}


console.log(differenceBy(groups, checkedgroups, 'id'))
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

You can use isEqualWith as follows:

_.isEqualWith(array, other, (objValue, othValue) => {
  if(objValue.id === otherValue.id && objValue.name === otherValue.name){
   return true'
  }
})
Umair Farooq
  • 1,763
  • 13
  • 16
0

If it's just a JSON string

try{
    return JSON.stringify(groups) === JSON.stringify(checkedgroups);
} catch(e) {
    return false
}
hoythan
  • 466
  • 5
  • 8
0

If you want to get the number of items which differ from one array to another you can map both your objects to an array of id's (or whatever you set the prop argument to). You can then use .filter to get the difference of arr1 - arr2. Lastly, you can then return the .length of the difference between the two arrays.

See working example below:

const groups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}],
checkedgroups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}, {id: 3, name: "check 3"}];

const differenceBy = (arr1, arr2, prop) => {
  const arrProp1 = arr1.map((obj) => obj[prop]),
  arrProp2 = arr2.map((obj) => obj[prop]),
  
  dif = arrProp1.filter(num => !arrProp2.includes(num));
  return dif.length;
}

console.log(differenceBy(checkedgroups, groups, "id"));
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

To compare any objects with each other (which have no circular reference) use these:

If you're sure whether they ain't null and undefined:
JSON.stringify(groups) === JSON.stringify(checkedgroups)

Otherwise:
JSON.stringify(groups || null) === JSON.stringify(checkedgroups || null)

Keyrad
  • 460
  • 4
  • 11