I have an array like this:
var movies = [
{ Name: "The Red Violin", ReleaseYear: "1998", Director: "François Girard" },
{ Name: "Eyes Wide Shut", ReleaseYear: "1999", Director: "Stanley Kubrick" },
{ Name: "The Inheritance", ReleaseYear: "1976", Director: "Mauro Bolognini" }
];
I want to find the location of the movie that’s released in 1999.
Should return 1.
What’s the easiest way?
Thanks.
You will have to iterate through each value and check.
Since JavaScript has recently added support for most common collection operations and this is clearly a filter operation on a collection, instead you could also do:
assuming you’re not interested in the indexes but the actual data objects. Most people aren’t anyways 🙂
.filteris not supported in all browsers, but you can add it yourself to your code base:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter#Compatibility