检查数组是否与另一个数组匹配以返回不匹配的内容

时间:2018-12-18 03:07:21

标签: javascript arrays

如果一个数组中的内容与另一个数组相同并且匹配了多个,我想将一个数组与另一个数组进行匹配,例如:

array2 = [1,2,3];
array1 = [1,3];
console.log(findMatch(array1,array2));
//result["total_match"] = 2 
//result["not_match"] = [2]

array2 = [4,5,6];
array1 = [6,4,5];
console.log(findMatch(array1,array2));
//result["total_match"] = 3
//result["not_match"] = []

array2 = [1,4,7];
array1 = [4,2,3,8,5];
console.log(findMatch(array1,array2));
//result["total_match"] = 1
//result["not_match"] = [1,7]

function findMatch(array1,array2){ // fill 
  var result = []; 
  result["total_match"] = 0;
  result["not_match"] = [];
  //????
  return result;  
}

基本上,array2是一个3索引数组,我想与另一个动态array1匹配,我想获得总匹配的结果,以及一个不匹配的值。数组

7 个答案:

答案 0 :(得分:5)

在这里使用Array.prototype.filter回答 https://medium.com/@alvaro.saburido/set-theory-for-arrays-in-es6-eb2f20a61848

给出的示例:

let intersection = arrA.filter(x => arrB.includes(x));

答案 1 :(得分:1)

您可以在函数内使用Array.reduce,然后使用Array.filter + Array.includes

const findMatch = (a,b) => a.reduce((acc,c) => {
  acc.total_match = a.filter(x => b.includes(x)).length
  acc.not_match = a.filter(x => !b.includes(x))
  return acc
}, {total_match: 0})


console.log(findMatch([1,2,3], [1,3]))
console.log(findMatch([4,5,6], [6,4,5]))
console.log(findMatch([1,4,7], [4,2,3,8,5]))

另一种选择是使用Array.forEach + Array.includes,然后跳过第二个filter

const findMatch = (a,b) => {
  let result = { total_match: 0, not_match: [] }
  a.forEach(x => !b.includes(x) ? result.not_match.push(x) : result.total_match++)
  return result
}

console.log(findMatch([1,2,3], [1,3]))
console.log(findMatch([4,5,6], [6,4,5]))
console.log(findMatch([1,4,7], [4,2,3,8,5]))

答案 2 :(得分:0)

我一直在等待有关“ not_match”标准的问题的答案,但似乎您只是提供了错误的预期结果。确切的代码就是这样(归功于Paul Thomas):

const notMatch = array2.filter(x => !array1.includes(x));
result["total_match"] = array2.length - notMatch.length;
result["not_match"] = notMatch;

答案 3 :(得分:0)

这是代码,

let difference = arrA
             .filter(x => !arrB.includes(x))
             .concat(arrB.filter(x => !arrA.includes(x))); // shows code that doesn't match

let difference = arrA.filter(x => !arrB.includes(x)); //shows code that matches

答案 4 :(得分:0)

伙计们,我很确定在JavaScript中您可以对数组进行==比较。

const areArraysEqual = arr1 == arr2;

答案 5 :(得分:0)

尝试一下

array1 = [1,4,7];
array2 = [4,2,3,8,5];
console.log(findMatch(array1,array2));

function findMatch(a1,a2){
    var result = {}
    var total_match = 0
    for (var i = 0; i< a2.length ;i++){
      if(a1.indexOf(a2[i]) != -1){
        total_match +=1
        removeA(a1,a2[i])
      }
    }
    result["total_match"] =total_match;
    result["not_match"] = a1;
    
    return result;
}

function removeA(arr) {
    var what, a = arguments, L = a.length, ax;
    while (L > 1 && arr.length) {
        what = a[--L];
        while ((ax= arr.indexOf(what)) !== -1) {
            arr.splice(ax, 1);
        }
    }
    return arr;
}

答案 6 :(得分:0)

  • concat()合并数组
  • filter()使用indexOf()使用符合此条件的合并数组:

    mergedArray.indexOf(element) !== index;
    

    这将返回匹配元素的数组。

  • 接下来使用filter()使用此条件indexOf()合并数组:

    matchedArray.indexOf(element) === -1;
    

    这将返回唯一元素的数组。

function arrayFilter(array1, array2) {
  var merged = array1.concat(array2);
  var matched = merged.filter(function(ele, idx, arr) {
    return arr.indexOf(ele) !== idx;
  });
  var uniques = merged.filter(function(ele) {
    return matched.indexOf(ele) === -1;
  });
  return `
  Matched: ${matched} -- Qty: ${matched.length}
  Uniques: ${uniques} -- Qty: ${uniques.length}`;
}

console.log(arrayFilter([1, 4, 7], [4, 2, 3, 8, 1]));
console.log(arrayFilter([33, 205, 7, 88, 1, 56], [4, 205, 3, 88, 1, 0]));
console.log(arrayFilter([3, 5, 17, 16, 101, 8], [8, 25, 3, 8, 99, 101]));
console.log(arrayFilter([0, 55, 8], [55, 0, 8]));
console.log(arrayFilter([111, 59, 4], [577, 97]));