In the example below, height returns an array of height values. The map function takes a function with one argument (in this case, image), which is a reference to an array element. The return value of this function will be used as an item in the resulting array.
var images = [ { height: '34px', width: '39px' }, { height: '54px', width: '19px' }, { height: '83px', width: '75px' }, ]; var heights = images.map(function(image){ return image.height; }); console.log(heights);
Here’s the same function using the arrow notation, which is more concise:
var images = [ { height: '34px', width: '39px' }, { height: '54px', width: '19px' }, { height: '83px', width: '75px' }, ]; var heights = images.map((image)=>{return image.height}); console.log(heights);
In the following example, the pluck function generates an array of property values. The first argument is an array of objects and the second argument is the property name.
var trips = [ { distance: 34, time: 10 }, { distance: 90, time: 50 }, { distance: 59, time: 25 } ]; function pluck(array, property) { return array.map(function(item){ return item[property]; }); } console.log(pluck(trips,'distance'));