1

I have the following array, of which I'm trying to perform a search for a particular element:

grid ({"row": row, "col": col})

Can anyone provide advice on how to locate an element in this array? I've tried something like this, but I can't seem to figure out if I'm doing it correctly.

grid.indexOf([row, col])

Any advice would be great.

1
  • The first code line - that's not an array ;) Commented Feb 2, 2015 at 12:30

4 Answers 4

2

This isn't an array, it's an object.

For an array you could just use:

array.forEach(function(element, index, array) {});

Since this is an object however you need to do a little more work:

Object.keys(grid).forEach(function(element, index, array) {
  if(element === some_random_name) {
    // Do whatever you need to here.
  }
});
Sign up to request clarification or add additional context in comments.

5 Comments

Actually, for searching through an array, Array.prototype.filter() or Array.prototype.find() would be a better fit. The first one is widely supported (IE9 and upwards).
for searching the object i would use: for (var element in grid) { if (element === 'search_name') {} }
@BrendanMullins—that requires a hasOwnProperty test, Object.keys is a better approach.
@RobG well.. no you dont. this for loops through all properties and if none match it wont execute the code inside the if.. so to test it again is redundant *edit: try it: jsfiddle.net/orttb64g/1
@BrendanMullins—for..in iterates over all enumerable properties on the object and its [[Prototype]] chain, so you might match an inherited property rather than an own property. Object.keys avoids that. It may not be required for an ad hoc solution, but it is for a general case.
0

Inside of your array you are keeping objects, so comparing them only by value does not work in JS.

You can do it the old fasion way (iterate):

function indexOfCell(row, col){
  for(var i=0; i< grid.length;i++){
     if(grid[i].row === row && grid[i].col === col){ // this code can get complex, when working with many properties
       return i;
     }
  }
  return -1;
}

Second solution would be filter from Array prototype:

function filterByRowAndCol(element) {
  if (element.row === row && element.col === col) {
    return element;
  } else {
    invalidEntries++;
  }
}

var arrByRowAndCol = grid.filter(filterByID);

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

1 Comment

I assume that grid is an array of objects with row and col properties.
0

This is an object, to access the element in your object you can use like this

grid = {"row": 10, "col": 12 };
alert( grid.row ); //will alert 10
alert( grid.col ); //will alert 12

Comments

0

You're working with an object, not an array.

You'll also need to refine your syntax for the object to work with it correctly. Try this: var grid = {'row': 'row', 'col': 'col'};

Then, you can loop through your object properties to find what you need:

var findValue = function (someKey) {
    for (var property in grid) {
        if (grid.hasOwnProperty(property)) {
            if (property === someKey) {
                return grid.property // return the value
            }
        }
    }
};

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.