js将对象转成多个子数组
Posted 不想掉头发啊!!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js将对象转成多个子数组相关的知识,希望对你有一定的参考价值。
方法一、常规for循环
// 对象 res = [lat: 23.080585479736328, lon: 23.080585479736328, value: "0.1",…]
// 存储对象转数组
let arr1 = [];
// 存储数组分割成子数组
let arr2 = [];
// 将对象转换为数组
for (let i in res)
for (let j in res[i])
arr1.push(res[i][j]);
//结果: arr1= [23.080585479736328,23.080585479736328,0.1,....]
// 将数组分割成三个为一组的子数组
for (let z = 0; z < arr1.length; z += 3)
arr2.push(arr1.slice(z, z + 3));
//结果: arr2 = [[23.080585479736328,23.080585479736328,0.1],[...],[...]]
方法二、利用map、Object.values()
const res = [
lat: 23.080585479736328,
lon: 23.080585479736328,
value: "0.1"
,
lat: 23.080585479736328,
lon: 23.080585479736328,
value: "0.2"
,
lat: 23.080585479736328,
lon: 23.080585479736328,
value: "0.6"
]
const newRes = res.map(item =>
return Object.values(item)
)
console.log(newRes)
// [[23.080585479736328,23.080585479736328,"0.1"],[23.080585479736328,23.080585479736328,"0.2"],[23.080585479736328,23.080585479736328,"0.6"]]
以上是关于js将对象转成多个子数组的主要内容,如果未能解决你的问题,请参考以下文章