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.

3
  • You want to flatten array 1 and ensure that each of it's values are also in array 2? Commented Jun 13, 2017 at 0:58
  • No, I want to make sure atleast one set of array in array 1 exists in array 2. Commented Jun 13, 2017 at 1:03
  • so loop and loop Commented Jun 13, 2017 at 1:20

5 Answers 5

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.
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You. I'll check that out.
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 );

7 Comments

should use every instead of forEach
@epascarello yes, you're right; every is a good candidate here. updated, thanks!
@vol7ron, So it returns false and true J? BTW beautiful code.
Thank You. I'll check that out
@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])
|
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)

3 Comments

Suppose i flatten my array 1, array 1 = 1,2,3,4,5; And my array 2=1,3,4 ; That would return true. But that wouldn't​ match any of my array set.
What i mean by an array set is (1,2,3) and (1,4,5) are 2 sets, If elements in my array 2 match any one of the sets, it should be true.
Thank You. I'll check that out
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)

2 Comments

Stop iterating the nested array when innerMatch got false. It saves your time.
Thank You. I'll check that out
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 ...

3 Comments

thank you. but ild like to know how to access "match" outside of the function.
i can see "true" inside the console log but how to i access it else where in html?
@vaibhavshah iv'e edited the answer ... you can append the array to the document onload ...

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.