-1

I would like to know how can I create keys for an array of values in javascript like this?

const array = ["NodeJs", "Javascript", "React"]

and transform it like this

const array = [{"name": "NodeJs"}, {"name": "Javascript"}, {"name": "React"}]
0

2 Answers 2

3

Using Array.prototype.map:

const arr = ["NodeJs", "Javascript", "React"];
const newArr = arr.map(name => ({name})); // or arr.map(item => ({name: item}))

console.log(newArr);

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

Comments

1

You can also do it using Array.from(iterable, mappingFunction) which will return a new array with objects {name: name}:

const array = ["NodeJs", "Javascript", "React"];
const mapArray = Array.from(array, (name) => ({name}));
console.log(mapArray);

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.