H5W3
当前位置:H5W3 > 其他技术问题 > 正文

数组中插入数组

问题描述

如何生成数组包数组的格式的数据,将pointa数组中的location元素取出,并弄成数字类型,放入新的数组

我的数据
`
“points”: [{

    "accuracy": 37.52,
"direction": 0,
"height": 175.47,
"locatetime": 1593320683164,
"location": "120.374254,36.088865",
"speed": 0
},
{
"accuracy": 36.448,
"direction": 0,
"height": 175.46,
"locatetime": 1593320685163,
"location": "120.374254,36.088865",
"speed": 0
},
{
"accuracy": 32.16,
"direction": 0,
"height": 175.43,
"locatetime": 1593320687164,
"location": "120.374254,36.088865",
"speed": 0
}]
`

###想要的数据类型
`
[

[120.23, 26.45],
[120.23, 26.45],
[120.23, 26.45],
[120.23, 26.45],

]
`

回答

points.map(v => v.location.split(',').map(k=>Number(k)))

//输出
 [
     [120.374254, 36.088865]
     [120.374254, 36.088865]
     [120.374254, 36.088865]
 ]

函数式编程,拿走不谢

points.map(point => point.location.split(",").map(n => Number(n)))
points.map(item => item.location).map(item => item.split(',').map(t => Number(t)))
const result = points.map(item => {
    // 如果需要精确到小数点后两位,使用tofixed,并再数字化即可,具体可看demo
  return item.location.split(',').map(every => Number(every))
})

demo 链接

let point = []
points.map(item=>{
  let arr = []
  arr.push(item.location)
  point.push(arr)
})

本文地址:H5W3 » 数组中插入数组

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址