将JavaScript对象映射到哈希数组中

将JavaScript对象映射到哈希数组中,javascript,ecmascript-6,Javascript,Ecmascript 6,我想获取一个Javascript对象并将其转换为一个哈希数组 以下操作仅获取对象的一个元素并将其转换为数组: const coordinatesArray = items.map((item) => item.latitude) 返回:[51.5165328979492,51.5990409851074,51.5990409851074,51.5165328979492,51.5098190307617,51.5128326416016,51.5098190307617,51.50176

我想获取一个Javascript对象并将其转换为一个哈希数组

以下操作仅获取对象的一个元素并将其转换为数组:

const coordinatesArray = items.map((item) => item.latitude)
返回:
[51.5165328979492,51.5990409851074,51.5990409851074,51.5165328979492,51.5098190307617,51.5128326416016,51.5098190307617,51.501766204834,51.51408767702,51.4983825683594,51.5294952392578,51.5123977776661133,51.501186370846,51.520487390137,51.51408777002,5177051.77031]

但当我尝试创建散列元素以组成数组时,我得到一个错误:

const coordinatesArray = items.map((item) => { x:item.latitude, y:item.longitude })
返回:
Uncaught错误:模块生成失败:语法错误:意外标记,应为


我做错了什么?

在花括号周围需要一些括号,否则会在中被解释为block语句

具有分解结构和简短属性的简短内容:

const coordinatesArray = items.map(({ latitude: x, longitude: y }) => ({ x, y }));

在花括号周围需要一些括号,否则它将在中解释为block语句

具有分解结构和简短属性的简短内容:

const coordinatesArray = items.map(({ latitude: x, longitude: y }) => ({ x, y }));
请尝试以下操作:

const coordinatesArray = items.map((item) => ({ x:item.latitude, y:item.longitude }))
返回对象的Lambda函数需要一个额外的括号集()来将它们与函数体区分开来。

请尝试以下操作:

const coordinatesArray = items.map((item) => ({ x:item.latitude, y:item.longitude }))

返回对象的Lambda函数需要一个额外的括号集()来将它们与函数体区分开来。

将函数体括起来以返回对象文字表达式:

 params => ({foo: bar}) 
就你而言:

const coordinatesArray = items.map((item) => ({ x:item.latitude, y:item.longitude }))

更多信息。

将函数体括起来以返回对象文字表达式:

 params => ({foo: bar}) 
就你而言:

const coordinatesArray = items.map((item) => ({ x:item.latitude, y:item.longitude }))

更多信息。

还有一个;在结尾处缺失的还有一个;最后的思念,非常感谢。成功了!非常感谢你。成功了!