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?

Recommended Answers

All 2 Replies

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.

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]]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.