2

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

4
  • 1
    Where is your attempt? Commented Dec 18, 2018 at 3:10
  • @jmargolisvt i'm stuck with that findMatch function 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.org/en-US/docs/Web/JavaScript/Reference/… 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. Commented Dec 18, 2018 at 3:42

7 Answers 7

5

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

The example given:

let intersection = arrA.filter(x => arrB.includes(x));
Sign up to request clarification or add additional context in comments.

2 Comments

this will return all value that matches, not returl value that not matches
@mileven negate the condition and you have your no matches. Add arrA.length -intersection.length and you have also the number of matches.
1

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

1 Comment

@mileven This is +1 set of operations (slower) compared to my answer...
1

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

Comments

0

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;

Comments

0

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

Comments

0

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

const areArraysEqual = arr1 == arr2;

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.