(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– Diodeus - James MacFarlane2013年12月17日 22:30:56 +00:00Commented Dec 17, 2013 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: stackoverflow.com/questions/8670345/… and stackoverflow.com/questions/8668174/…James Wilson– James Wilson2013年12月17日 22:34:48 +00:00Commented Dec 17, 2013 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– James Wilson2013年12月17日 22:35:59 +00:00Commented Dec 17, 2013 at 22:35
-
even if there was a native method it too would have to loop over the arraycharlietfl– charlietfl2013年12月17日 22:37:39 +00:00Commented Dec 17, 2013 at 22:37
-
2Thanks 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– Matthew Marsee2013年12月17日 22:50:11 +00:00Commented Dec 17, 2013 at 22:50
1 Answer 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;
};