'Find matching values in two arrays

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.



Solution 1:[1]

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

Solution 2:[2]

use th find to check element exist

firstArray.forEach(secondElement=>{
  let exist = secondArray.find((item) => item === secondElement);
  if(exist){
    jsonArray.push({'name': secondElement, 'matched': true})
  }else{

    jsonArray.push({'name': secondElement, 'matched': false})
  }
});

Demo

Solution 3:[3]

We can use

Array.prototype.includes()

to check if an element exist or not in an array

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

firstArray.forEach(val=>{

if(secondArray.includes(val))
{
  jsonArray.push({'name': val, 'matched': true})

}else{
  jsonArray.push({'name': val, 'matched': false})
}

})

console.log(jsonArray);

Solution 4:[4]

Try this:

  let firstArray = ["One", "Two", "Three". "Four", "Five"];
  let secondArray = ["Three", "Four"];
  let jsonArray = [];
  firstArray.forEach(firstElement=>{
        if(secondArray.indexOf(firstElement)>=0){
            jsonArray.push({'name': firstElement, 'matched': true});
        }else{
            jsonArray.push({'name': firstElement, 'matched': false});
        }
 });

Hope this code will help you

Solution 5:[5]

You can do this in two way.

Way1:
let array1 = ['a','b','c','d'];
let array2 = ['b','d'];
let commonArray = [];

array1.forEach( x => {
    if(array2.indexOf(x) != -1){
      commonArray.push(x);
    }
});`

console.log(commonArray);   //commonArray is the resulting array

Way2: Use intersection of Underscore.js

At first you have to import underscore:  
import * as _ from 'underscore';`  

Then use intersection to calculate common.  
commonArray = _.intersection(array1, array2);

console.log(commonArray);   //commonArray is the resulting array

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Heehaaw
Solution 2 Sachila Ranawaka
Solution 3 dhaker
Solution 4
Solution 5