Join the Stack Overflow Community
Stack Overflow is a community of 6.2 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

I have an array with objects, each object, a div, has an id. Can I retrieve the array index number of a given object based on its id using this?

var index = ar.map(function(el) {
  return el. ??
}).indexOf('objectID4'); 
share|improve this question

marked as duplicate by procrastinator, CD.., Wladimir Palant, Frédéric Hamidi, icodebuster Dec 18 '13 at 8:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
possible duplicate of stackoverflow.com/q/19111224/1636522 – procrastinator Dec 18 '13 at 7:39

Like this:

var index = ar.map(function(el) {
  return el.id;
}).indexOf('objectID4');

working example: http://jsfiddle.net/bvT6B/

share|improve this answer
    
I tried that, I'm getting -1. and I know that objectID4 exists for sure – user3024007 Dec 18 '13 at 7:29
    
@user3024007 May be case sensitivity kicks in – thefourtheye Dec 18 '13 at 7:33
    
yes, I see... I have to figure out why my live code is not taking it – user3024007 Dec 18 '13 at 7:42

check for below code

var ar = [{'id':'objectId1','foo':'bar'},{'id':'objectId4','foo':'bar'}]

var index = ar.map(function(el) {
   return el.id;
}).indexOf('objectId4');

alert(index);

Or you can see in http://jsfiddle.net/VJrWc/

If this is not you want the share some more code and clear your goal.

share|improve this answer

It this what you want?

<div id="x1" class='foo'></div>
<div id="x2" class='foo'></div>
<div id="x3" class='foo'></div>

<div id="res"></div>
<script>
function getIndexOfById(uid) {
    var id = null;
    $('.foo').each(function() {
        if ($(this).attr('id') == uid) {id = $(this).index();}
    });
    return id;
}

$('#res').html(getIndexOfById('x1') + ' / ' + getIndexOfById('x2') + ' / ' + getIndexOfById('x3'));
</script>

The result is

0 / 1 / 2
share|improve this answer
    
$('#x1').index('.foo')!!! – procrastinator Dec 18 '13 at 7:29

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