0
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"]
    },

];

using this array ,reate an array using forEach that has all the usernames with a "!" to each of the usernames:

let newArray = []
array.forEach(user => {
    let { username } = user;
    username = username + "!";
    newArray.push(username);
})

console.log(newArray);

i couldnt understand this syntax. i mean why did we use {} after let ?

parliamed
  • 1
  • 1
  • 8
    [Destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) – Andy Jun 28 '22 at 13:05
  • basically `let { username } = user` is the same as `let username = user.username` – eroironico Jun 28 '22 at 13:06

2 Answers2

1

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

Alex
  • 81
  • 3
0

Using { } after let is known as object deconstruction. It is a better way of writing let username = user.username;. You can learn more about it from here

Eric
  • 69
  • 5