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

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

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

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.

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.

Comments

Your Answer

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.