Home > OS >  Covert array to array of objects
Covert array to array of objects

Time:12-01

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}]`

CodePudding user response:

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

CodePudding user response:

Sure, you can do it with code like this using the Array map function

let x = ["frontalCard","backCard"]
let y = x.map(a => ({ [a]: false }))
console.log(y)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related