Rehan H
Rehan H

Reputation: 314

Match Arrays Value To Return All Matched Occurrences

I'm in need of little help with records matching in two arrays with different length.

Requirements:

Example:

    var findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];

    var findTheseValues =  ["Apple", "Mango"];

    Returned Array = ["Apple", "Apple("Matched")", "Orange", "Mango", "Mango("Matched")", "Mango", "Mango(Matched)", "Apple("Matched")"];

    **// Push matched values next to the value that was matched in the FindFrom Array**

I tried:

var findFrom = ["Apple", "Mango", "Orange", "Banana", "Orange", "Orange","Orange"];
​
var findTheseValues = ["Orange", "Banana"];
​
for(let i = 0; i < findFrom.length; i++){
​
    if (findFrom[i] === findTheseValues[i] // toString() if required){
        console.log(findFrom[i]);
    }
}

If I just replace the 'i' in the if condition for find These Values with 0, it returns the matched values but I don't want it to match just one value - it should loop over both arrays.

Tried Find from ES 6 but it just returns one value that matched.

I'm happy to explain more if required & I appreciate the help! :)

Upvotes: 2

Views: 1742

Answers (6)

Marco Luzzara
Marco Luzzara

Reputation: 6036

I am writing this answer because all other solutions proposed are based on linear searching over findTheseValues array, whereas the total computational complexity can be definitely reduced using a different data structure: I am talking about Sets or Maps. They are probably implemented using a hash table and as ECMAScript Standard says:

must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection.

So, my solution is almost equal to the others, except for data structure used.

let findTheseValuesSet = new Set(findTheseValues);
let newArray = [];

findFrom.forEach(elem => {
    newArray.push(elem);
    if (findTheseValuesSet.has(elem))
        newArray.push(elem + '(Matched)');
});

Upvotes: 0

Fantastisk
Fantastisk

Reputation: 488

I would do something like this

var findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];
var findTheseValues =  ["Apple", "Mango"];

var matches = findFrom.map((e, i)=>{
  return r = findTheseValues.includes(e) ? e + '(matched)' : e;
});

console.log(matches);

Upvotes: 0

Mohammad Usman
Mohammad Usman

Reputation: 39322

You can use .map() and .includes() methods:

let findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"],
    findTheseValues =  ["Apple", "Mango"];

let result = findFrom.map(s => s + (findTheseValues.includes(s) ? ` (${s})` : ''));

console.log(result);

Docs:

Upvotes: 1

Chad Moore
Chad Moore

Reputation: 764

Here's a decent solution that uses native Array methods instead of for-loops, which is always a better idea, imho.

Also, this will also allow you to look for the same value more than once, e.g. include a value more than once in your search.

let findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];

// try either of the following
let findTheseValues =  ["Apple", "Mango"];
//let findTheseValues =  ["Apple", "Mango", "Mango"];

let results = [];
findFrom.forEach((i) => {
  const match = findTheseValues.filter((a) => i === a);
  const result = match.length > 0 ? match.concat(i) : i;
  results.push(i);
  if (match.length > 0) {
    match.forEach(m => results.push(m));
  };
});

console.log(results);

Upvotes: 0

Manoj
Manoj

Reputation: 1195

You can use Arrays map method which return a new set of array with matched text appended.

    var returnedArray = findFrom.map((ele)=> {
if(findTheseValues.indexOf(ele)!==-1){
   return [ele, ele+"(matcheed)"] // return a pair with appended matched
}else{
  return ele;
}
}).join().split(',') // to flatten array.

Upvotes: 0

chevybow
chevybow

Reputation: 11908

This solution works for me:

  var findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];

  var findTheseValues =  ["Apple", "Mango"];

  var solution = [];
  for(let i = 0; i < findFrom.length; i++){
    solution.push(findFrom[i]);
    if (findTheseValues.indexOf(findFrom[i]) !== -1) {
      solution.push(findFrom[i] + ' (Matched)');
    }
  }

console.log(solution)

This loops through the array, tries to find the element at the index in "findthesevalues"- then pushes the "matched" string to the new array if we find a match

Upvotes: 0

Related Questions