Javascript find in array

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 a movie released in 1999.

Must return 1 .

What is the easiest way?

Thanks.

+4
source share
6 answers

You will need to iterate over each value and check.

 for(var i = 0; i < movies.length; i++) { if (movies[i].ReleaseYear === "1999") { // i is the index } } 

Since JavaScript recently added support for the most common collection operations , and this is clearly a filter operation in the collection, you can also do this instead:

 var moviesReleasedIn1999 = movies.filter(function(movie) { return movie.ReleaseYear == "1999"; }); 

assuming you are not interested in indexes, but actual data objects. Most people are not always :)

.filter not supported in all browsers, but you can add it to your code base: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter#Compatibility

+6
source

Built? Use loops.

Do you want to get a fantasy? Linq to Javascript: http://jslinq.codeplex.com/

+2
source

Sort of:

 function findMovieIndices(movies, prop, value) { var result = []; for(var i = movies.length; i--; ) { if(movies[i][prop] === value) { result.push(i); // personally I would return the movie objects } } return result; } 

Using:

 var indices = findMovieIndices(movies, "ReleaseYear", "1999"); 

Perhaps this gives you an idea of ​​a more generalized function (if you need it).

+2
source

Since you also marked it with jQuery, you can use the map function:

 var movies = $.map(movies,function(item,index){ return item.ReleaseYear == 1999 ? index : null; }); 

This will return an array of indexes for all films since 1999. If you want the movies themselves to be an array:

 var movies = $.map(movies,function(item){ return item.ReleaseYear == 1999 ? item : null; }); 
+2
source

If functional style programming is applicable:

_.indexOf(_.pluck(movies, "ReleaseYear"), "1999")

Because it is so simple. The functional underscore.js toolkit can be very powerful.

_.indexOf , ._pluck

+1
source

You will need to create your own search function.

 Array.prototype.findMovieByYear = function (findYear) { for (var i = 0; i < this.length; i++) { // this actually returns the element, maybe you just want // to return the array index ( the i param ) if (this[i].Release == findYear) return this[i]; } return null; // or return -1 or whatever if you want to return the index }; // now you can call: movies.findMovieByYear('1998'); // and that should return { Name: "The Red Violin", ReleaseYear: "1998", Director: "François Girard" } 

Of course, this way of doing this actually affects every array you create. Perhaps this is not what you want .. you can create your own array object, then ...

0
source

Source: https://habr.com/ru/post/1338357/


All Articles