0

I want to convert an Array like:

[ 'John', 'Jane' ]

into an array of object pairs like this:

 [{'name': 'John'}, {'name':'Jane'}]

Please help me to do so..

3

2 Answers 2

10

Try the "map" function from the array:

const output = [ 'John', 'Jane' ].map(name => ({name}));
console.log(output);

Sign up to request clarification or add additional context in comments.

2 Comments

About why parentheses should be added to return values: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
It's short for this. const output = ['John', 'Jane'].map(name=>({name: name}));
0

You can use the instance method .map() on a JS list Object as follows :

 let list = ['John', 'Jane']
 list = list.map(x => {
    return({name: x});
                      });
 console.log(list);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.