Skip to content
Advertisement

Create an array using forEach that has all the usernames

This is an array and I want to use foreach to create an array using forEach that has all the usernames with a “!” to each of the usernames

JavaScript

What i try in JS:

JavaScript

What i got in error:

Uncaught SyntaxError: Unexpected token .

What is the right way to use foreach to access the object in the array?

Advertisement

Answer

You can do it with forEach like so (note using var not const because you can’t modify const):

JavaScript

However, it’s much simpler to use map:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement