Mikey
Mikey

Reputation: 396

Searching a 2D Javascript Array For Value Index

I am trying to write a jQuery that will find the index of a specific value within a 7x7 2D array.

So if the value I am looking for is 0 then I need the function to search the 2D array and once it finds 0 it stores the index of the two indexes.

This is what I have so far, but it returns "0 0" (the initial values set to the variable.

Here is a jsFiddle and the function I have so far:

http://jsfiddle.net/31pj8ydz/1/

$(document).ready( function() {

    var items = [[1,2,3,4,5,6,7], 
                 [1,2,3,4,5,6,7], 
                 [1,2,3,0,5,6,7], 
                 [1,2,3,4,5,6,7], 
                 [1,2,3,4,5,6,7], 
                 [1,2,3,4,5,6,7], 
                 [1,2,3,4,5,6,7]];

    var row = 0;
    var line = 0;

    for (i = 0; i < 7; ++i) {
        for (j = 0; i < 7; ++i) {
            if (items[i, j] == '0,') {
                row = i;
                line = j;
            }
        }
    }

    $('.text').text(row + ' ' + line);
});

HTML:

<p class="text"></p>

Upvotes: 1

Views: 575

Answers (7)

user3303864
user3303864

Reputation:

Looks like you are still learning how to program. But here is an algorithm I've made. Analyze it and compare to your code ;)

var itens = [[1,2,3,4,5,6,7], 
             [1,2,3,4,5,6,7], 
             [1,2,3,0,5,6,7], 
             [1,2,3,4,5,6,7], 
             [1,2,3,4,5,6,7], 
             [1,2,3,4,5,6,7], 
             [1,2,3,4,5,6,7]];

var row = null;
var column = null;

for (var i = 0; i < itens.length; i++) {
    for (var j = 0; j < itens[i].length; j++) {
        if (itens[i][j] == 0) {
            row = i;
            column = j;
        }
    }
}

console.log(row, column);

Upvotes: 0

Mritunjay
Mritunjay

Reputation: 25882

There are following problems in the code

1) items[i,j] should be items[i][j].

2) You are comparing it with '0,' it should be 0 or '0', if you are not concerned about type.

3) In your inner for loop you should be incrementing j and testing j as exit condition.

Change your for loop like bellow and it will work

for (i = 0; i < 7; i++) {
        for (j = 0; j < 7; j++) {
            if (items[i][j] == '0') {
                row = i;
                line = j;
            }
        }
    }

DEMO

Note:-

1) Better to use === at the place of ==, it checks for type also. As you see with 0=='0' gives true.

2) Better to say i < items.length and j<items[i].length instead of hard-coding it as 7.

Upvotes: 0

Christos
Christos

Reputation: 53958

You access your array with the wrong way. Please just try this one:

items[i][j]

When we have a multidimensional array we access the an element of the array, using array[firstDimensionIndex][secondDimensionIndex]...[nthDimensionIndex].

That being said, you should change the condition in your if statement:

 if( items[i][j] === 0 )

Please notice that I have removed the , you had after 0. It isn't needed. Also I have removed the ''. We don't need them also.

Upvotes: 1

sagar43
sagar43

Reputation: 3456

You are doing loop wrong On place of

 for (i = 0; i < 7; ++i) {
        for (j = 0; i < 7; ++i) {
            if (items[i, j] == '0,') {
                row = i;
                line = j;
            }
        }
    }

use this

 for (i = 0; i < 7; i++) {
        for (j = 0; j < 7; j++) {
            if (items[i][j] == 0) {
                row = i;
                line = j;
            }
        }
    }

Here is the demo

Upvotes: 0

Jared Smith
Jared Smith

Reputation: 21926

var foo;
items.forEach(function(arr, i) {
    arr.forEach(function(val, j) {
        if (!val) { //0 coerces to false
            foo = [i, j];
        }
    }
}

Here foo will be the last instance of 0 in the 2D array.

Upvotes: 0

forgivenson
forgivenson

Reputation: 4435

First, you are accessing your array wrong. To access a 2D array, you use the format items[i][j].

Second, your array doesn't contain the value '0'. It doesn't contain any strings. So the row and line variables are never changed.

You should change your if statement to look like this:

if(items[i][j] == 0) {

Notice it is searching for the number 0, not the string '0'.

Upvotes: 1

Marko Gresak
Marko Gresak

Reputation: 8207

Your if statement is comparing

if (items[i, j] == '0,')

Accessing is wrong, you should use [i][j].

And your array has values:

[1,2,3,4,5,6,7]
....

Your value '0,' is a string, which will not match numeric values inside the array, meaning that your row and line won't change.

Upvotes: 1

Related Questions