Find matching values in two arrays

12,051

Solution 1

You can use the combination of map and includes helper to achieve that.

let firstArray = ["One", "Two", "Three", "Four", "Five"];
let secondArray = ["Three", "Four"];
let jsonArray = [];

jsonArray = firstArray.map(i => {
   return { 'name': i, 'matched': secondArray.includes(i) };
});

console.log(jsonArray);

Solution 2

All the other solutions here perform unnecessary computations; their run-time grows with the square of the array length. Try running them with arrays of size 100k+ :-)

The solution you are looking for is quite simple and runs in O(n):

let firstArray = ["One", "Two", "Three", "Four", "Five"];
let secondArray = ["Three", "Four"];

let map = {};
firstArray.forEach(i => map[i] = false);
secondArray.forEach(i => map[i] === false && (map[i] = true));
let jsonArray = Object.keys(map).map(k => ({ name: k, matched: map[k] }));
Share:
12,051
sie
Author by

sie

Updated on June 04, 2022

Comments

  • sie
    sie almost 2 years

    I want to find the matching values between two arrays and create a json array setting true if the values matched or false if they didn't. I know, that the values in the secondArray will always match some values from the first array and that it will always be smaller, because the secondArray is created based on the first one.

    let firstArray = ["One", "Two", "Three", "Four", "Five"];
    let secondArray = ["Three", "Four"];
    let jsonArray = [];
    

    I want to create a json array:

    [
      {
        "name": "One",
        "matched": false
      },
      {
        "name": "Two",
        "matched": false
      },
      {
        "name": "Three",
        "matched": true
      },
      {
        "name": "Four",
        "matched": true
      },
      {
        "name": "Five",
        "matched": false
      }
    ]
    

    Normally, I would do something like this:

                firstArray.forEach(firstElement=>{
                  secondArray.forEach(secondElement=>{
                      if(firstArray.indexOf(secondElement)>=0){
                          jsonArray.push({'name': secondElement, 'matched': true});
                      }else{
                          jsonArray.push({'name': secondElement, 'matched': false});
                      }
                  });
              });
    

    But this just creates a json array with duplicated values where the name is the same, but the matched value are falsed and true.

    It seems like I'm getting lost in something very simple.

    • Philipp Sander
      Philipp Sander almost 6 years
      you have a lot of typos in your code
    • Philipp Sander
      Philipp Sander almost 6 years
      you have to merge both array and set matched to false, then ou have to intersect both array and set values from the intersect to matched true in your merged array
  • Siddharth Sarfale
    Siddharth Sarfale almost 6 years
    @PhilippSander where is typo in my code could you please let me know?
  • Philipp Sander
    Philipp Sander almost 6 years
    inexOf should be indexOf
  • sie
    sie almost 6 years
    All the answers are correct, but this one has less code. Approved.
  • Roel
    Roel almost 6 years
    @sie well in that case you can check my answer ;)
  • sie
    sie almost 6 years
    I did, but now I saw map and return, and it is prettier.