var users = [
{firstName: 'Pete', lastName: 'Barrat', favoriteFood: 'Pizza', age: 30},
{firstName: 'Lisa', lastName: 'Jenkins', favoriteFood: 'Curry', age: 34},
{firstName: 'Bob', lastName: 'Yates', favoriteFood: 'Fish', age: 54},
{firstName: 'Claire', lastName: 'Smith', favoriteFood: 'Steak', age: 21},
{firstName: 'Adam', lastName: 'Johnson', favoriteFood: 'Pasta', age: 27}
];
function valueMapper(){
for(var i=0; i < users.length; i++)
return users; //is this where I'm going wrong?
};
console.log(valueMapper('firstName'));
5 Answers
If I understand you correctly you need to get firstName's of list of objects. You can get this using .map on array.
var users = [{
firstName: 'Pete',
lastName: 'Barrat',
favoriteFood: 'Pizza',
age: 30
}, {
firstName: 'Lisa',
lastName: 'Jenkins',
favoriteFood: 'Curry',
age: 34
}, {
firstName: 'Bob',
lastName: 'Yates',
favoriteFood: 'Fish',
age: 54
}, {
firstName: 'Claire',
lastName: 'Smith',
favoriteFood: 'Steak',
age: 21
}, {
firstName: 'Adam',
lastName: 'Johnson',
favoriteFood: 'Pasta',
age: 27
}];
function valueMapper(key) {
return users.map(function(item) {return item[key]});
}
console.log(valueMapper('firstName'));
Comments
var users = [
{firstName: 'Pete', lastName: 'Barrat', favoriteFood: 'Pizza', age: 30},
{firstName: 'Lisa', lastName: 'Jenkins', favoriteFood: 'Curry', age: 34},
{firstName: 'Bob', lastName: 'Yates', favoriteFood: 'Fish', age: 54},
{firstName: 'Claire', lastName: 'Smith', favoriteFood: 'Steak', age: 21},
{firstName: 'Adam', lastName: 'Johnson', favoriteFood: 'Pasta', age: 27}
];
function valueMapper(key){
var resultArray = [];
for(var i=0; i < users.length; i++) {
resultArray.push(users[i][key])
}
return resultArray;
};
console.log(valueMapper('firstName'));
Comments
So i guess you want to map all users to the attribute you specified as parameter. Try this:
var users = [
{firstName: 'Pete', lastName: 'Barrat', favoriteFood: 'Pizza', age: 30},
{firstName: 'Lisa', lastName: 'Jenkins', favoriteFood: 'Curry', age: 34},
{firstName: 'Bob', lastName: 'Yates', favoriteFood: 'Fish', age: 54},
{firstName: 'Claire', lastName: 'Smith', favoriteFood: 'Steak', age: 21},
{firstName: 'Adam', lastName: 'Johnson', favoriteFood: 'Pasta', age: 27}
];
function valueMapper(attribute) {
return users.map(u => u[attribute]);
};
console.log(valueMapper('firstName'));
var users = [
{firstName: 'Pete', lastName: 'Barrat', favoriteFood: 'Pizza', age: 30},
{firstName: 'Lisa', lastName: 'Jenkins', favoriteFood: 'Curry', age: 34},
{firstName: 'Bob', lastName: 'Yates', favoriteFood: 'Fish', age: 54},
{firstName: 'Claire', lastName: 'Smith', favoriteFood: 'Steak', age: 21},
{firstName: 'Adam', lastName: 'Johnson', favoriteFood: 'Pasta', age: 27}
];
function valueMapper(key){
//let's use array.prototype.map here:
return users.map(function(element){
return element[key];
});
}
console.log(valueMapper('firstName'));
The way to go here is Array.prototype.map. It iterates over all the elements and returns the results in an array.
More explanation here:
The array is filled with Objects ({..}). Every object has keys and values. These keys are addressable by using square brackets [..] since we want them to be variable. Users itself is an array, so we can use array functions like map on them. Map iterates over each array item and returns something when a condition is met. In this case there is no special condition, only that we want to return a specific key.
Leroy Jenkinswould've have been funnier for the second entry.