简体   繁体   English

两个 arrays javascript 之间的区别

[英]difference between two arrays javascript

I've seen more than one question solving this but when applying them it didn't work for me, please help me solve this.我已经看到不止一个问题解决了这个问题,但是在应用它们时它对我不起作用,请帮我解决这个问题。 I've written my code according to this question我已经根据这个问题编写了我的代码

    let difference = selectedStores.filter(selectedStore => {
      alreadySyncedStores.filter(productSyncedStores => {
        console.log('productSyncedStores', productSyncedStores);
        console.log('selectedStore', selectedStore);
        console.log('result ', !productSyncedStores.includes(selectedStore));
        !productSyncedStores.includes(selectedStore)
      })
    });
    console.log('>>difference<<<', difference)

both arrays and console results are reflected in the images below thanks in advance. arrays 和控制台结果都反映在下面的图像中提前感谢。 在此处输入图像描述

expected result is: difference = ['100000000713']预期结果是:差异 = ['100000000713']

You can use this function to get the desired output Suppose you have two array您可以使用此 function 来获得所需的 output假设您有两个数组

var arr1 = ['12','32','54']; var arr1 = ['12','32','54']; var arr2 = ['14','32','21']; var arr2 = ['14','32','21'];

function GetDifference(arr1,arr2) { function GetDifference(arr1,arr2) {

var diffArr = []; var diffArr = [];

arr1.forEach(function (index, value){ arr1.forEach(函数(索引,值){

if (arr2.indexOf(index) == -1) {

    diffArr.push(index);

}

}); });

arr2.forEach(function (index, value){ arr2.forEach(函数(索引,值){

if (arr1.indexOf(index) == -1 && diffArr.indexOf(index) == -1) {

    diffArr.push(index);

}

}); });

return diffArr;

}` }`

You Will Get out as below.你会得到如下。

// ['12', '54', '14', '21'] // ['12', '54', '14', '21']

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

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