I have an array with different objects inside. I want to write a function pluck
which takes the array & a property and return an array of all the values of that property in the different objects.
I have tried this:
var paints = [
{color: 'red'},
{color: 'yellow'},
{color: 'blue'},
];
function pluck(arr, property) {
return arr.map(function(obj, property) {
return obj[property];
});
}
console.log(pluck(paints, 'color'));
This does not work.
If I change the function like so:
function pluck(arr) {
return arr.map(function(obj) {
return obj['color'];
});
...it works, but it is obviously hard-coded now. However, I would like to call the function and specify which property I want to have returned in an array.