简体   繁体   English

如何检查javascript中两个数组是否有区别?

[英]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. 有StackOverflow上一个类似的问题在这里的这个问题,我们需要得到数组中的差异。

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 我的解决方案: _.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. 如果元素不相等,则filter返回的数组为空。 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: 您可以获取id的数组以比较数组的字符串版本:

 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

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

If it's just a JSON string 如果只是JSON字符串

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). 如果要获得从一个数组到另一个数组的项数不同,可以将两个对象都映射到id的数组(或将prop参数设置为的数组)。 You can then use .filter to get the difference of arr1 - arr2 . 然后,您可以使用.filter来获取arr1 - arr2的差值。 Lastly, you can then return the .length of the difference between the two arrays. 最后,您可以返回两个数组之间的差异的.length

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: 如果您确定它们是否不是null和undefined:
JSON.stringify(groups) === JSON.stringify(checkedgroups)

Otherwise: 除此以外:
JSON.stringify(groups || null) === JSON.stringify(checkedgroups || null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 object 的数组是否存在于 Javascript 的另一个 object 数组中 - How to check whether an array of object is present in another array of object in Javascript JavaScript 中如何检查两个对象的匹配数据是否很少 - How to check whether two Objects has few of the matching data in JavaScript 如何检查数组元素是否存在于Javascript字符串中? - How to check whether an element of array exists in string in Javascript? 如何使用 JavaScript 检查项目在数组中是否唯一 - How to check whether the items are unique in an array using JavaScript 如何使用JavaScript检查字符串是否为数组字符串的一部分 - how to check whether a string is part of an array string using JavaScript 如何检查Javascript数组中是否存在多个值 - How to check whether multiple values exist within an Javascript array 如何检查数组中的特定项目是否有值? Java脚本 - How to check whether specific items inside an array have a value? Javascript Javascript:检查键是否存在于对象数组中 - Javascript: Check whether key exists in an array of objects 检查javascript中数组中的所有值是否相同 - check whether all the values in array is same or not in javascript 检查字符串中是否存在 list(array) 中的两个或多个单词...javascript - check whether two or more words from list(array) are present in the string...javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM