0

I am having trouble checking the contents of an array contained within a main array.

Example:

I have two arrays

var main = [[1,2,3],
         [4,5,6]];
var compare = [1,2,4,5,6]

I want to compare the array "compare" with each array within the array "main" to see if it contains any of the numbers. The result would be something I could then test against (boolean or the index position).

I tried indexOf and couldn't figure it out.

Edit This should still return true:

var main = [[1,2,3], // returns false
           [4,5,6], // returns false
           [7,8,9], // returns true
           [2,3,7]]; // returns true

var compare = [2,3,4,6,7,8,9]

** Update w/ Solution ***

I needed to check if compare array's contents matched any of the subarrays in main. Here's what I came up with:

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

var counter = 0;
var counter2 = 0;

var compare = [4,1,3,2];

for (var i = 0; i <= compare.length; i++) {
// Sorting 
compare.sort();

if (main[0].indexOf(compare[i]) > -1) {
    counter++;
    console.log("Added a point to counter 1");
} else if (main[1].indexOf(compare[i]) > -1) {      
    counter2++;
    console.log("Added a point to counter 2");
} else {   
  console.log("No points added");
}
}

// if any of the counters have 3 marks, then the player hit it 3 times.
if (counter == 3 || counter2 === 3){
console.log("A counter is at 3");
}

Any feedback on what I came up with? What's a better way of doing this?

1
  • 2
    For loops + indexOf should help here. Commented Oct 3, 2014 at 22:23

4 Answers 4

1

You'll need 2 loops, the first to iterate over your array of arrays, the next to check for existing elements within the current array:

for (var i = 0; i < main.length; i++) {
    for (var j = 0; j < main[i].length; j++) {
        if (compare.indexOf(main[i][j]) {
            //compare has a number from the current array! main[i][j] exists in compare!
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's returning a error: TypeError: Cannot read property 'length' of undefined
0

Take a look at lodash library, the have that exact functionality written already

2 Comments

Why use a library if three lines of very simple code can do the job?
That is true, you don't need a library to do this. How ever good libraries have good unit test coverage and take into account browser differences. I'm all for pure javascript an all, but there is no reason to reinvent the wheel ether...
0

You can use built-in array methods:

var result = main.map(function(xs) {
  return xs.some(function(x) {
    return compare.indexOf(x) > -1
  })
})

It will return [true, true]

Comments

0

Following, two of the possible solutions:

Suppose:

var main = [[1,2,3],
            [4,5,6],
            [7,8,9],
            [2,3,7],
            [5,1,10]];

var compare = [2,3,4,6,7,8,9];

First solution: return true if any element of the main inner array is included in the master one which is compare:

var result1= main.map(function(element,index,array){
    return element.reduce(function(previousValue, currentValue, index, array){
        return (previousValue || (compare.indexOf(currentValue) >= 0));
    }, false);
});

This solution gives the following result:

result1 = [true,true,true,true,false]

Second solution: return the index of the main inner array elements in compare:

var result2= main.map(function(element,index,array){
    return element.map(function(element,index,array){
        return (compare.indexOf(element));
    });
});

This solution gives the following result:

result2 = [[-1,0,1],[2,-1,3],[4,5,6],[0,1,4],[-1,-1,-1]]

Check this link jsfiddle to see a working example.

Hope it's useful!

Comments

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.