1
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'));
Mouser
13.3k3 gold badges31 silver badges54 bronze badges
asked Jan 3, 2017 at 13:17
1
  • Leroy Jenkins would've have been funnier for the second entry. Commented Jan 3, 2017 at 13:32

5 Answers 5

1

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'));

answered Jan 3, 2017 at 13:25
Sign up to request clarification or add additional context in comments.

Comments

0

 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'));

answered Jan 3, 2017 at 13:23

Comments

0

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'));

answered Jan 3, 2017 at 13:21

4 Comments

Is he using a ES6 your suggestion may be false
It's not php mate: u => u[attribute])
Do you guys mean the arrow function? I don't see the problem.
Yup we do, fails miserably on almost every browser.
0

You are just returning the whole users array instead of performing your value check and only returning one element. Also your valueMapper function takes no arguments with this definition. You should start with reading JS basics since there are a lot of problems in this example.

answered Jan 3, 2017 at 13:21

Comments

0

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.

answered Jan 3, 2017 at 13:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.