JavaScript
Article

jQuery Array Search Simple Example

By Sam Deering

Quick example on how you can use jQuery’s $.map() function to grab a value from an array of JS objects. In the example I have an array of JS objects that contain days and prices, we want to extract a price for a specific day.

var dayArr = Array( { "day" : "day01", "price" : "$210" }, { "day" : "day02", "price" : "$220" }, { "day" : "day03", "price" : "$230" } );

var findDay = 'day02'; //find price for day 1

var price = $.map(dayArr, function(value, key) {
     if (value.day == findDay)
     {
        return value.price;
     }
});
console.log(price);
//output: $220

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

No Reader comments

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.