0

I am new to coding and im having trouble with arrays in javascript.

I want to be able to compare stuff inside an array but only if the three values match up to one of the sets of three values in the array

var array = [[1,2,3][4,5,6][3,6,7]]
if(array.indexOf([1,2,3]) === 1){

}

but when I do this it doesn't work does anyone know why?

3
Contributors
2
Replies
18
Views
1 Year
Discussion Span
Last Post by AndrisP
0

Your syntax has a lot of mistakes in it which you are hopefully aware of. But even without them it didn't work as you had it.
This works:

var array = [];
    var obj = [1,2,3];
    var obj2 = [4,5,6];
    var obj3 = [7,8,9];
    array.push(obj);
    array.push(obj2);
    array.push(obj3);
    console.log(array);
    console.log(array.indexOf(obj2)); // gives position of 1

So I think the object creation you used is the problem. Although your if statement would have failed regardless because the object you looked for would have been at position zero not position 1. Arrays are zero based in javascript.

0

Missed comma between arrays
var array = [[1,2,3][4,5,6][3,6,7]]
replace to
var array = [[1,2,3],[4,5,6],[3,6,7]]

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.