0

I want to compare an array with an array set. For example,

array 1 = [[1,2,3],[1,4,5]];
array 2 = [1,3,6,5,4];

Since the elements 1,4,5 in array 2 match with a set in array 1 it should return true.

5 Answers5

1

Iterate over your array and check if the value exists in the other array.

var sets = [
    [1, 2, 3],
    [1, 4, 5]
  ],
  valid = [1, 3, 6, 5, 4];

var processed = sets.map(set => set.every(val => valid.includes(val)));

console.log( processed);

There are ways to make this more efficient, but try this for starters.


Here's an example how you can check if any are true:

// Check if any are true
var any = processed.some(v=>v);
console.log( any );
vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • should use every instead of forEach – epascarello Jun 13 '17 at 01:49
  • @epascarello yes, you're right; `every` is a good candidate here. updated, thanks! – vol7ron Jun 13 '17 at 01:54
  • @vol7ron, So it returns false and true J? BTW beautiful code. – Kosh Jun 13 '17 at 02:11
  • Thank You. I'll check that out – Vaibhav Shah Jun 13 '17 at 02:18
  • @KoshVery yes, it's uncertain the desired behavior. With all values it can be determined if any are true with `processed.some(v=>v)`, or if wanted to retrieve all the matching sets `processed.map((v,i)=>v?i:null).filter(v=>v!==null).map(i=>sets[i])` – vol7ron Jun 13 '17 at 03:02
  • @vol7ron, so don't you think that it's too much overhead? Two loops look more efficient. – Kosh Jun 13 '17 at 03:11
  • @KoshVery if efficiency is the game, we're talking about bit masks, typed arrays and caching. I'm saying the answer provided is sufficient because the question doesn't express intention. What I meant in the comment is that the `processed` array is now flexible to cater to do anything. If the real intention is to determine if any array in `sets` returns true, then a standard for-loop would be desired so that the loop can break early as soon as one is encountered. But yes flexibility vs performance; without more information I think flexibility is typically better. – vol7ron Jun 13 '17 at 10:58
1

use loop and loop. get all child array in array1, and check each child array include in array2.

function check(){
  var array1 = [[1,2,3],[1,4,5]];
  var array2 = [1,3,6,5,4];
  for(let arr of array1){
    let flag=true;
    for(let child of arr){
      if(array2.indexOf(child) < 0){
        flag = false;
        break;  // if one element not in array2, enter next loop.
      }
    }
    if(flag) return flag; // if got one child array elements all in array2, stop loop.
  }
}
0

Flatten the first array (unpack the nested arrays). Then do an intersection of the flattened array and the second array. Map through the first array, and for each each array do an intersection against the second array. Then filter out all arrays that are empty. If the resulting array contains something, then something matched.

const hasMatch = Boolean(array1.map(a => intersect(a, array2)).filter(i => i.length).length)
Joseph
  • 117,725
  • 30
  • 181
  • 234
0

var array1 = [
  [1, 2, 3],
  [1, 4, 5]
];


var array2 = [1, 3, 6, 5, 4];

var isMatch = doesNestedArrayMatchArray(array1, array2);

function doesNestedArrayMatchArray(nestedArray, bigArray) {
  var atLeastOneMatch = false;

  for (var i = 0; i < nestedArray.length; i++) {
    var innerMatch = true;

    //go through each array of array1
    for (var j = 0; j < nestedArray[i].length; j++) {
      if (bigArray.indexOf(nestedArray[i][j]) == -1){
         innerMatch = false;
          break;
        }
    }

    if (innerMatch) atLeastOneMatch = true;
  }

  return atLeastOneMatch;
}

console.log(isMatch)
Cavan Page
  • 525
  • 2
  • 12
0

You can iterate over array1 and use every() on each number group and as a condition for it use includes() with the second array for a relatively short syntax:

var array1 = [
  [1, 2, 3],
  [1, 4, 5]
];
var array2 = [1, 3, 6, 5, 4];
var results = [];

array1.forEach(function(item, index, array) {
  if (item.every(x => array2.includes(x))) {
    results.push(item)
  }
});

console.log(results)

Edited : I'm pushing the results that return true to an empty array ...

Gal Ratzkin
  • 124
  • 2
  • 8