简体   繁体   中英

How to map to an object using the values of an array of object?

Hi i have the following json response

[{ country: string, code: string}]

now i would like to create an object with array.map(x => {[x.country]: false}) only the syntax [] to use the value of country as a key does not work. the purpose is to have a dynamic checkbox group fetched from the backend How do map the array to { "country": false }

The brackets behind the arrow belong to the function and not to the object you try to create.

Add either ()

array.map(x => ({[x.country]: false}))

or a return value.

array.map(x => {
  return {[x.country]: false};
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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