Sriker Ch Sriker Ch - 6 months ago 13
Javascript Question

Filtering in JavaScript or jQuery

I am trying to take the data from the JSON file (link to file).
If I have to sort by the name or by the year of release, how to filter
the contents in that 2D array.

Can anyone please help me to sort this out.

Answer

Using jQuery.get() to get your api response, and than sort using Array.prototype.sort().

Sort by Year:

$.get("http://www.omdbapi.com/?s=Batman&page=2", (result) => {
    result.Search.sort((a, b, by) => {
        return a.Year < b.Year ? -1 : a.Year > b.Year ? 1 : 0;
    });
    console.log(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>