16

i have this array of strings :

let myArray : ["AA","BB" , "CC" ...]

I want to convert it to an array of objects:

myArray  = [{"id":1 , "value": "AAA"},{"id":2 , "value": "BBB"},{"id":3 , "value": "CCC"}...]

I ve trie with "let for":

for (let obj of  ListObj) {
      let resObj = {};
      resObj ['value'] = obj  ;
      equipment = resObj ;
}

And with map :

ListObj.map(obj => { 'value' = obj })

Suggestions ?

1 Answer 1

47

You can use .map() for this. It passes the index into the callback.

myArray = myArray.map((str, index) => ({ value: str, id: index + 1 }));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.