Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
156
rated 0 times [  162] [ 6]  / answers: 1 / hits: 17684  / 6 Years ago, tue, october 9, 2018, 12:00:00

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 ?


More From » angular

 Answers
1

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



myArray = myArray.map((str, index) => ({ value: str, id: index + 1 }));

[#53346] Thursday, October 4, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondaytond

Total Points: 92
Total Questions: 88
Total Answers: 96

Location: China
Member since Fri, Jan 15, 2021
3 Years ago
dylondaytond questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Thu, May 7, 20, 00:00, 4 Years ago
;