简体   繁体   English

将数组转换为对象数组

[英]Covert array to array of objects

I would like to know if there is a way to do this我想知道是否有办法做到这一点

there is a way to transform this `["frontalCard","backCard"]` to `[{"frontalCard": false},{"backCard": false}]`有一种方法可以将此`["frontalCard","backCard"]`转换为`[{"frontalCard": false},{"backCard": false}]`

stringArray.map(s => {
  const o = {};
  o[s] = false;
  return o;
}

Sure, you can do it with code like this using the Array map function当然,您可以使用 Array map function

 let x = ["frontalCard","backCard"] let y = x.map(a => ({ [a]: false })) console.log(y)

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

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