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.
If you want the names you can’t use the spread operator, that is what is spreading each string in characters
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.
Do you know what those 3 dots before nameArr
do?