0

I need to check if an incoming numerical array matches any variation of a set of numerical arrays. I am basically just stuck on the logic.

Given

var myData = [1, 201, 100]

Are these three numbers found in any order in the following pre-set combinations

var combo1 = [1, 100, 200]
var combo2 = [1, 101, 201]
var combo3 = [1, 100, 201]
var combo4 = [1, 101, 200];

My attempt so far is going nowhere so I've reduced it to this https://jsfiddle.net/0mvk9dj4/1/

var myData = [1, 201, 100];

var combo1 = [1, 100, 200]
var combo2 = [1, 101, 201]
var combo3 = [1, 100, 201]
var combo4 = [1, 101, 200];
var combos = [combo1, combo2, combo3, combo4];

function findCombo(data) {
  var found = false
  for (var i = 0; i < combos.length; i++) {
    var combo = combos[i];
    for (var x = 0; x < combo.length; x++) {
      for (var y = 0; y < data.length; y++) {
        if (data[y] === combo[x]) {
          found = true;
          break;
        } else {
          found = false;
        }
      }
    }
  }
  console.log("Found? " + found)
  return found;
}

findCombo(myData);
2
  • Are combo1-4 fix, i.e. not subject to change during code execution? Commented Mar 20, 2017 at 1:45
  • Indeed they are fixed Commented Mar 20, 2017 at 1:49

2 Answers 2

2

The findCombo function in the code below uses a functional approach to solve your problem. Feel free to read more about the .every() and .some() methods on the Mozilla Developer Network.

var myData = [1, 201, 100];

var combo1 = [1, 100, 200];
var combo2 = [1, 101, 201];
var combo3 = [1, 100, 201];
var combo4 = [1, 101, 200];

var combos = [combo1, combo2, combo3, combo4];

function findCombo(data, combos) {
  return combos.some(function(combo) {
    return combo.every((item) => data.includes(item));
  });
}

console.log(findCombo(myData, combos));
Sign up to request clarification or add additional context in comments.

1 Comment

I found this to be the fastest of the answers. Thanks! jsperf.com/array-combo-check/1
1

You can use .findIndex() or .filter(), .every(), .some()

var myData = [1, 201, 100];

var combo1 = [1, 100, 200]
var combo2 = [1, 101, 201]
var combo3 = [1, 100, 201]
var combo4 = [1, 101, 200];
var combos = [combo1, combo2, combo3, combo4];
var res = combos.filter(combo =>  combo.every(n => myData.some(curr => curr === n)));

console.log(res);

var res = combos.findIndex(combo =>  combo.every(n => myData.some(curr => curr === n)));

console.log(combos[res]);

3 Comments

myData is the input to be tested, so wouldn't it be something like myData.every(n => combos.some(combo => combo.includes(n)))?
@nnnnnn Not presently at computer where can check for certain. Approach at Answer should return index of matching array within combos array
Maybe we're just approaching the same thing from opposite angles. I put the .every() on myData because I was thinking of it as "every element of myData is in [one of the combos]", but all of the arrays are the same length so doing it the opposite way is probably fine. Though now that I look at it I think my logic is faulty in that it probably is checking that each element in myData is in some array, not all in the same array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.