[英]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.