简体   繁体   English

如何使用对象数组的值映射到对象?

[英]How to map to an object using the values of an array of object?

Hi i have the following json response嗨,我有以下 json 响应

[{ 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.现在我想用array.map(x => {[x.country]: false})创建一个对象,只有语法[]使用 country 的值作为键不起作用。 the purpose is to have a dynamic checkbox group fetched from the backend How do map the array to { "country": false }目的是从后端获取一个动态复选框组 如何将数组映射到{ "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};
})

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

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