1

(First off, I've been reading questions and answers here for a long time but this is my first post.)

I've found solutions that involve writing your own function for it, but I have to believe there's a built-in way to do this.

I'm working in JavaScript and have an array of custom objects, each of which look like this:

{ prop1: "1", prop2: "blah", prop3: "news", prop4: "2", prop5: "1" }
{ prop1: "2", prop2: "foo", prop3: "news", prop4: "2", prop5: "1" }
{ prop1: "3", prop2: "bar", prop3: "news", prop4: "2", prop5: "1" }
{ prop1: "4", prop2: "hello", prop3: "news", prop4: "2", prop5: "1" }

I want to find a built-in way to find the array index of one of the objects given the value of one of its properties (for this example, if I gave it "prop2" and "bar" it would return index 2). It would be nice to use .indexOf like you'd expect, but apparently with an array of custom objects, it doesn't work that way.

I've found code for a function of my own that works fine, but in my stubbornness I'm convinced there's just got to be a built-in way to do it. I'm doing a ton of stuff in jQuery in this project, so that's absolutely an option as well. Any suggestions?

  • Take a look at Backbone.js. You create models for your data schema, and create collections out of them which you can query. – Diodeus - James MacFarlane Dec 17 '13 at 22:30
  • I don't believe there is a native means of doing JSON queries without writing your own function (like @scrblnrd3's example) or using a library. This question has been asked many times in many ways. Examples: http://stackoverflow.com/questions/8670345/trouble-using-indexof-on-a-complex-array and http://stackoverflow.com/questions/8668174/indexof-method-in-an-object-array – James Wilson Dec 17 '13 at 22:34
  • Also, keep in mind, indexOf is meant to be used on native javascript types, not object/complex types. So technically, indexOf *does* work as expected. With all due respect, I believe your expectation is wrong. – James Wilson Dec 17 '13 at 22:35
  • even if there was a native method it too would have to loop over the array – charlietfl Dec 17 '13 at 22:37
  • _" but I have to believe there's a built-in way to do this"_ - Not yet. However, in the EcmaScript 6 proposal, there's an [`Array.findIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex). You aren't going to find that on many browsers yet (except FireFox). – Ted Hopp Dec 17 '13 at 22:41
  • 2
    Thanks to everyone for the very prompt responses. JamesWilson, you're right that it's been asked several times and I did find several of the answers but I was more curious about a built-in solution. And you're also right that my expectation is probably wrong but I sort of felt that because type-safety is not typically common (or easy/trivial) in JavaScript, that it would treat everything the same way. charlietfl, that's true, it would have to iterate over the array, but performance really doesn't matter much in this example. @TedHopp - That's more like what I was looking for. Thanks! – Matthew Marsee Dec 17 '13 at 22:50

1 Answers1

1

You can extend the native array prototype to do this, with both property and value as strings. Here is a JSFiddle to demonstrate that

Array.prototype.indexOfAssociative=function(property,value){
    for(var i=0;i<this.length;i++){
          if(this[i][property]==value){
               return i;
          }

    }
    return -1;
};
scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
  • Right, that's the solution I've taken to do it but I just have to believe there's a built-in way to do it. It's possible I'm wrong, I guess. – Matthew Marsee Dec 17 '13 at 22:31
  • It is better not to extend native objects as it violates incapsulation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FInheritance_and_the_prototype_chain#Bad_practice.3A_Extension_of_native_prototypes – HMR Dec 17 '13 at 23:38