Can't understand why loop push later, i need field name in my array

console.clear();

function list(names){
  let arr = [];
    for(var key in names){
        let nameArr = names[key].name;
       arr.push(...nameArr);
    }
  console.log(arr)
}
list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])

What do you want to get?
With your code right now arr is:

["B", "a", "r", "t", "L", "i", "s", "a", "M", "a", "g", "g", "i", "e"]

If you want to get only the names in the new array then why noy use a regular for loop? You can access each object in the list array with dot notation alone.

1 Like

If you want the names you can’t use the spread operator, that is what is spreading each string in characters

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Do you know what those 3 dots before nameArr do?

1 Like