2

I have the following array:

var = array[
            {"id" : "aa", "description" : "some description"},
            {"id" : "bb", "description" : "some more description"},
            {"id" : "cc", "description" : "a lot of description"}]

and I try to find the index of the array that contains the id === "bb". The solution I came up with is the following:

var i = 0;
while(array[i].id != "bb"){
   i++;
}
alert(i) //returns 1

Is there an easier way that has cross-browser functionality? I tried $.inArray(id,array) but it doesn't work.

3
  • 3
    Easier? What part of that isn't already easy? (though you should add some validation to make sure the id exists) Commented Oct 1, 2013 at 8:20
  • I know I just think I have to avoid loops as much as possible ;) So I thought maybe there's a way.... Good point with the validation. A simple try/catch should do the job, right? Commented Oct 1, 2013 at 8:26
  • No need for try/catch. Just use a for loop instead of while and use -1 as the result if none is found - that seems to be common. I'll do you an answer I think Commented Oct 1, 2013 at 8:27

5 Answers 5

5

I don't see any problem with the complexity of your code, but I would recommend a couple of changes including adding some validation in case the value does not exists. Further more you can wrap it all in a reusable helper function...

function getArrayIndexForKey(arr, key, val){
    for(var i = 0; i < arr.length; i++){
        if(arr[i][key] == val)
            return i;
    }
    return -1;
}

This can then be used in your example like so:

var index = getArrayIndexForKey(array, "id", "bb");
//index will be -1 if the "bb" is not found

Here is a working example

NOTE: This should be cross browser compatible, and will also likely be faster than any JQuery alternative.

Sign up to request clarification or add additional context in comments.

1 Comment

This is buggy when refreshing browser. Need to refresh twice to get updated true index
2
var myArray = [your array];
var i = 0;

$.each(myArray, function(){
    if (this.id === 'bb') return false;
    i++;
})

console.log(i) // will log '1'

Update with modern JS.

let index
myArray.map(function(item, i){
    if (item.id === 'cc') index = i
})

console.log(index) // will log '2'

Comments

1

inArray can't work with multidimensional array so try like the following

var globalarray= [
            {"id" : "aa", "description" : "some description1"},
            {"id" : "bb", "description" : "some more description"},
            {"id" : "cc", "description" : "a lot of description"}];
var theIndex = -1;
for (var i = 0; i < globalarray.length; i++) {
    if (globalarray[i].id == 'bb') {
        theIndex = i;
        break;
    }
}
alert(theIndex);

Demo

Comments

1

You can use jQuery.each - http://api.jquery.com/jQuery.each/

var i;
jQuery.each(array, function(index, value){
   if(value.id == 'bb'){
      i = index;
      return false; // retrun false to stop the loops
   }
});

Comments

0
Object.keys(yourObject).indexOf(yourValue);

1 Comment

Please add an explanation so the OP understands why this code solves his problem.

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.