Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array

cells = [0, 0, 0, 0, 0, 0, 0, 0, 0];

which is updated with a jQuery function

 $(document).ready(function(){
...
    $('#box').click(function(e){

        var indexArray = e.target.id.split('_');

        if (indexArray.length > 1) {
            var index = indexArray[1];

            if (cells[index] == 0){
                cells[index] = move;
...
})

I want to make a cross-check of cells array groups. for example:

(cells[0] + cells[1] + cells[2]);   // row 1
(cells[3] + cells[4] + cells[5]);   // row 2
(cells[6] + cells[7] + cells[8]);   // row 3
...

I tried to create a multidimensional array, but all I get is undefined:

var triggers = [[cells[0], cells[1], cells[2]]];

is it possible to pass cells arrays' variables to triggers array? Can't figure it out?!

share|improve this question

1 Answer 1

up vote 5 down vote accepted

You can use slice to get part of an array, for example

var triggers = [cells.slice(0, 3)];

The call cells.slice(0, 3) returns an array with the elements of cells starting from index 0 up to and excluding 3, i.e. [cells[0], cells[1], cells[2]]. You can wrap another array over that "manually" to get the desired result.

share|improve this answer
    
Superb )) this is what i needed. Only one thing is left )) I also need to chekUp to diagonals )) cells[0] + cells[4] + cells[8] cells[2] + cells[4] + cells[6] –  A1exandr Oct 17 '13 at 10:02
    
@A1exandr: That won't be possible with an one-liner unless you write it out manually. –  Jon Oct 17 '13 at 10:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.