最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if array is match with another array to return what are not match - Stack Overflow

programmeradmin3浏览0评论

i want to match an array with another array if the content in array is same with another array and how many is match, for example:

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;  
}

basically the array2 is an array with 3-index that i want to match with another dynamic array1, i want to get the result of the total match, and the value that not match in an array

i want to match an array with another array if the content in array is same with another array and how many is match, for example:

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;  
}

basically the array2 is an array with 3-index that i want to match with another dynamic array1, i want to get the result of the total match, and the value that not match in an array

Share Improve this question edited Dec 18, 2018 at 3:43 mileven asked Dec 18, 2018 at 3:07 milevenmileven 2044 silver badges13 bronze badges 4
  • 1 Where is your attempt? – jmargolisvt Commented Dec 18, 2018 at 3:10
  • @jmargolisvt i'm stuck with that findMatch function – mileven Commented Dec 18, 2018 at 3:13
  • Asking us to write the function for you is unlikely to be as helpful as giving you feedback on a more fully developed attempt that you add to your question. Have you tried using map, reduce, or filtermethods? There are lots of great Array methods out there to help you: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – jmargolisvt Commented Dec 18, 2018 at 3:19
  • You've updated the question a bit. But still, the criteria to not_match are unclear, OR you just provided wrong examples. – Nurbol Alpysbayev Commented Dec 18, 2018 at 3:42
Add a ment  | 

7 Answers 7

Reset to default 5

This is answered here, using Array.prototype.filter https://medium./@alvaro.saburido/set-theory-for-arrays-in-es6-eb2f20a61848

The example given:

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

You could use a Array.reduce inside your function and then 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]))

Another option is to use Array.forEach + Array.includes and in this way skip the 2nd 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]))

try this,

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;
}

I've been waiting for answers to my questions about the criteria of "not_match", but it seems you've just provided wrong expected results. The exact code would be this (credits to Paul Thomas):

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

Here is the code,

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

Guys I'm pretty sure in JavaScript you can just do a == parison on the arrays.

const areArraysEqual = arr1 == arr2;

  • Merge the arrays with concat()
  • filter() the merged array with this criteria using indexOf():

    mergedArray.indexOf(element) !== index;
    

    This will return an array of matched elements.

  • Next filter() the merged array with this criteria using indexOf():

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

    This will return an array of unique elements.

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]));

发布评论

评论列表(0)

  1. 暂无评论