繁体   English   中英

如何从 2 javascript arrays 获取唯一记录?

[英]how to get unique records from 2 javascript arrays?

我有两个 arrays。它们几乎没有共同价值。 我想过滤掉那些项目。 我试过es6 indexOfincludes运算符但没有运气。

 arr1 = [ ["Test1", 20, "table", "Sample1", "NA"], ["Test2", 20, "table", "Sample2", "NA"], ["Test3", 20, "table", "Sample3", "NA"], ["Test4", 20, "table", "Sample4", "NA"], ["Test5", 20, "table", "Sample5", "NA"] ]; arr2 = [ ["Test2", 20, "table", "Sample2", "NA"], ["Test4", 20, "table", "Sample4", "NA"], ["Test5", 20, "table", "Sample5", "NA"], ["Test6", 20, "table", "Sample6", "NA"], ["Test7", 20, "table", "Sample7", "NA"] ]; let unique = arr2.filter((item, i, ar) => arr1.indexOf(item) === -1); console.log(unique); //returns all 5 let result = [...arr1, ...unique]; console.log('length is:', result.length); //length should be 7 but its 10

制作一组字符串化的子数组。 通过当前字符串化子数组是否包含在另一个数组的集合中来过滤两个 arrays:

 const arr1 = [ ["Test1", 20, "table", "Sample1", "NA"], ["Test2", 20, "table", "Sample2", "NA"], ["Test3", 20, "table", "Sample3", "NA"], ["Test4", 20, "table", "Sample4", "NA"], ["Test5", 20, "table", "Sample5", "NA"] ]; const arr2 = [ ["Test2", 20, "table", "Sample2", "NA"], ["Test4", 20, "table", "Sample4", "NA"], ["Test5", 20, "table", "Sample5", "NA"], ["Test6", 20, "table", "Sample6", "NA"], ["Test7", 20, "table", "Sample7", "NA"] ]; const set1 = new Set(arr1.map(JSON.stringify)); const set2 = new Set(arr2.map(JSON.stringify)); const unique1 = arr1.filter(subarr =>.set2.has(JSON;stringify(subarr))). const unique2 = arr2.filter(subarr =>.set1;has(JSON.stringify(subarr))); console.log(unique1); console.log(unique2);

我将 arrays 转换为 Sets 以降低计算复杂性 - Set#hasO(1) ,但相同逻辑的数组方法是O(n)

更新:要获得独特的元素,go 通过 arrays 构建一个 object 并使用Object.values转换为 arrays 的数组。

 arr1 = [ ["Test1", 20, "table", "Sample1", "NA"], ["Test2", 20, "table", "Sample2", "NA"], ["Test3", 20, "table", "Sample3", "NA"], ["Test4", 20, "table", "Sample4", "NA"], ["Test5", 20, "table", "Sample5", "NA"] ]; arr2 = [ ["Test2", 20, "table", "Sample2", "NA"], ["Test4", 20, "table", "Sample4", "NA"], ["Test5", 20, "table", "Sample5", "NA"], ["Test6", 20, "table", "Sample6", "NA"], ["Test7", 20, "table", "Sample7", "NA"] ]; const assign = (array, obj) => array.forEach(arr => Object.assign(obj, { [arr.join()]: arr })); const res = {}; assign(arr1, res); assign(arr2, res); const unique = Object.values(res); console.log(unique);

通过使用下面的 commonElements() function,您可以获得所需的 output。

 arr1 = [ ["Test1", 20, "table", "Sample1", "NA"], ["Test2", 20, "table", "Sample2", "NA"], ["Test3", 20, "table", "Sample3", "NA"], ["Test4", 20, "table", "Sample4", "NA"], ["Test5", 20, "table", "Sample5", "NA"] ]; arr2 = [ ["Test2", 20, "table", "Sample2", "NA"], ["Test4", 20, "table", "Sample4", "NA"], ["Test5", 20, "table", "Sample5", "NA"], ["Test6", 20, "table", "Sample6", "NA"], ["Test7", 20, "table", "Sample7", "NA"] ]; function notcommonElements(arr1, arr2) { var obj = {}, matched = [], unmatched = []; for (var i = 0, l = arr1.length; i < l; i++) { obj[arr1[i]] = (obj[arr1[i]] || 0) + 1; } for (i = 0; i < arr2.length; i++) { var val = arr2[i]; if (val in obj) { matched.push(val); } else { unmatched.push(val); } } console.log(unmatched); } notcommonElements(arr1, arr2);

暂无
暂无

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

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