5

I have array of objects

var arr = [
        {"id" : "1", "description" : "one"},
        {"id" : "2", "description" : "two"},
        {"id" : "3", "description" : "three"}]

I need get index, for example, for object with id="2".I do

var index = jQuery.inArray( {"id" : "2", "description" : "two"}, arr )

In index I get -1.

JsFiddle

3 Answers 3

9

Because inArray uses === to compare elements, and different objects are never === to one another. (They're also not == to one another.)

E.g.:

var a = {"foo": "bar"};
var b = {"foo": "bar"};
console.log(a === b); // "false"

You'll need to create a method on them to compare them for equivalence, and then do the search yourself.

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

Comments

5

You can use a function that takes a callback:

function arrayFind(arr, fn) {
    for( var i = 0, len = arr.length; i < len; ++i ) {
        if( fn(arr[i]) ) {
            return i;
        }
    }
    return -1;
}

var arr = [
        {"id" : "1", "description" : "one"},
        {"id" : "2", "description" : "two"},
        {"id" : "3", "description" : "three"}
];

var result = arrayFind(arr, function(v){
    return v.id === "2" && v.description === "two";
});
console.log(result) //1

Comments

1

As TJ said, inArray uses === (indexOf actually, but that's the same thing), therefore even identical literals are compared non equal. Here's a workaround:

var index = jQuery.inArray( 
    JSON.stringify({"id" : "2", "description" : "two"}), 
    $.map(arr, JSON.stringify) )

http://jsfiddle.net/7kg9P/1/

11 Comments

Not a good idea, the order of the properties is not significant in objects, but JSON.stringify may well output properties in different orders for different but equivalent objects: jsbin.com/ELAToPO/1 (Also: inArray only uses indexOf if the browser has it. Part of the point of inArray is that IE was missing Array#indexOf for a long time...)
@T.J.Crowder: note that I carefully used words "identical literals", not "equivalent objects", so you're fighting the wrong point here. As to IE7/8, jQuery, as of 2.0, doesn't support these anymore.
All due respect, but it makes no sense whatsoever to solve this problem only for identical literals and not equivalent objects. No production code should ever be delivered with such a dependency, and I'm sure you know that. As for jQuery 2.0, most people still can't use it except for intranet stuff, what with IE8 being 21% of the global browser market share. And that's going to be a long tail, thanks to IE9 not being available for XP.
@T.J.Crowder: I think you're being too pedantic here. My snippet demonstrates a quick and easy workaround for the problem and works just fine for the OP's data. Designing a universal complete deep comparator for JS objects is out of the scope of the question.
I completely disagree. This doesn't solve the OP's problem, not in a way that would work reliably in the real world. My concern is pragmatic, not pedantic. (I am a pedant, but I'm not being pedantic here. :-) )
|

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.