Javascript Map multiple values into list

I currently have an array … code goes like this:

namelist=[];

var namelist = mydata.cars.map( o => o.name );

This gives me the name of the cars.

What I want to do it to pass several values instead of just one so I get then call for them when needed.

For example id and name.

How can I do this?

Using the ES6 style you may have have a problem doing:

.map(o => {name: o.name, id: o.id})

Because the curly braces mean that you start a block, not an object.
If that is the problem you are having, you should use that block, with a return statement:

.map(o => { return {name: o.name, id: o.id} })

Or, create an object by using the constructor:

.map(o => new Object({name: o.name, id: o.id}))

UPDATE:

You can also do:

.map(o => ({name: o.name, id: o.id}))


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   How to round up to the nearest 100 in JavaScript

Similar Posts