Mapping An Array
Mapping An Array
Array
map() method
Another useful and powerful method we have in
JavaScript is the map() method.
● Value
● Index
● Array
Then we add the number, then finally the closing list item element.
With this markup, we can display each number using a bullet point.
Here you can see each number is now mapped to a string, that's our
list item.
Joining the
array
We now have an array of strings, that we can use the join() method
that we learned about earlier to join the elements of this array and
create a string.
Here we have a simple implementation, by adding opening and closing ul tags, and
concatenating the items in between these tags.
Mapping to an
object
Instead of mapping to a string, perhaps
you want to map to an object.
These methods are chainable, which means we can call them one after
another in a chain.
In our example, the filtered constant is only used with the map
method.
Instead, we can call the filter method, then immediately call the map
method.