-2

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

  • Yes there is. Using `map` you can iterate over each item and return an object with the key and the value false. https://stackoverflow.com/questions/2274242/how-to-use-a-variable-for-a-key-in-a-javascript-object-literal – evolutionxbox Nov 30 '21 at 21:35

2 Answers2

0

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)
Always Learning
  • 5,510
  • 2
  • 17
  • 34
-1
stringArray.map(s => {
  const o = {};
  o[s] = false;
  return o;
}
JSmart523
  • 2,069
  • 1
  • 7
  • 17
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/30473744) – Mahmut EFE Dec 02 '21 at 08:26
  • @MahmutEFE Please pay attention when reviewing, this is not a "link-only" answer. It is an answer. Due to the lack of information it might be downvote-worthy, but it is still an answer. – Mark Rotteveel Dec 02 '21 at 09:35
  • Thank you Mark. I was confused since there was not a single link in my answer. – JSmart523 Dec 02 '21 at 15:39