简体   繁体   English

JS:将字符串数组转换为对象数组

[英]JS : Convert Array of Strings to Array of Objects

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 ? 建议?

You can use .map() for this. 您可以使用.map() It passes the index into the callback. 它将索引传递给回调。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM