0
// Complete the below questions using this array:
const array = [
  {
    username: "john",
    team: "red",
    score: 5,
    items: ["ball", "book", "pen"]
  },
  {
    username: "becky",
    team: "blue",
    score: 10,
    items: ["tape", "backpack", "pen"]
  },
  {
    username: "susy",
    team: "red",
    score: 55,
    items: ["ball", "eraser", "pen"]
  },
  {
    username: "tyson",
    team: "green",
    score: 1,
    items: ["book", "pen"]
  },

];

//Create an array using forEach that has all the usernames with a "!" to each of the usernames

const exclamArray = [];
const newArray = array.forEach((username) => {
  exclamArray.push(array.username + '!');
})
console.log('forEach', exclamArray);

when i executed the code above, it gave me

forEach (4) ["undefined!", "undefined!", "undefined!", "undefined!"]

how can i select the object's value 'username'? have i done anything wrong on that code?

1
  • 1
    Use username, not array.username. Or rather, (player) => { … player.username … }. And don't use forEach, but map. Commented Jan 9, 2020 at 23:57

3 Answers 3

1

The first parameter of the callback function for Array.prototype.forEach is an element, not a property.

Set a variable for element and reference it with username. And if you want to save a new array to variable newArray by returning value, use a Array.prototype.map.

const array = [{
    username: "john",
    team: "red",
    score: 5,
    items: ["ball", "book", "pen"]
  },
  {
    username: "becky",
    team: "blue",
    score: 10,
    items: ["tape", "backpack", "pen"]
  },
  {
    username: "susy",
    team: "red",
    score: 55,
    items: ["ball", "eraser", "pen"]
  },
  {
    username: "tyson",
    team: "green",
    score: 1,
    items: ["book", "pen"]
  },

];
//Create an array using forEach that has all the usernames with a "!" to each of the usernames

const exclamArray = [];
const newArray = array.forEach((element) => {
  exclamArray.push(element.username + '!');
})
console.log('forEach', exclamArray);

Sign up to request clarification or add additional context in comments.

Comments

0

the forEach iterator will pass each element of an array into the executing function. In this case, the arrow function is receiving the element into the variable username. Moving forward, in order to access members of that element (since they are all objects), you would use username.username (for example, if you wanted to access the username of the object).

Hopefully it is clear that you may want to choose a different name in your arrow function's variable.

As a minor clarification, being that array is an array of objects, it does not have a username property, and each iteration of the forEach loop reflects undefined in the exclamArray as a result.

Comments

0

Array.map() makes the most sense for collecting the subparts into another array:

let usernames = users.map(user => user.username + '!')

["john!", "becky!", "susy!", "tyson!"]

Which really makes me wonder if they're wanting you to do the following:

users.forEach(user => user.username += '!')

0: {username: "john!", team: "red", score: 5, items: Array(3)}
1: {username: "becky!", team: "blue", score: 10, items: Array(3)}
2: {username: "susy!", team: "red", score: 55, items: Array(3)}
3: {username: "tyson!", team: "green", score: 1, items: Array(2)}

https://jsfiddle.net/7fq2n0wp/3/

What that does is modify the original array usernames by reference.

Comments

Your Answer

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