简体   繁体   中英

How to check whether there is difference in two array in javascript?

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

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)) 

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')) 

You can use isEqualWith as follows:

_.isEqualWith(array, other, (objValue, othValue) => {
  if(objValue.id === otherValue.id && objValue.name === otherValue.name){
   return true'
  }
})

If it's just a JSON string

try{
    return JSON.stringify(groups) === JSON.stringify(checkedgroups);
} catch(e) {
    return false
}

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")); 

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM