假如 我数据是这样式的 一个数组里边包含两个对象
var data = [{ "gender": "female", "height": 161.2, "weight": 51.6 }, { "gender": "female", "height": 167.5, "weight": 59 }]
想转换成二维数组【两种方法】
for循环遍历 再push一下
var data = [{ "gender": "female", "height": 161.2, "weight": 51.6 }, { "gender": "female", "height": 167.5, "weight": 59 }, ]
console.log(data);
// 处理数据 要求二维数组
let axdata = []
for(var i = 0; i<data.length;i++){
let height = data[i].height
let weight = data[i].weight
let newArr = [height,weight]
axdata.push(newArr)
}
console.log(axdata);
用map
var data = [{ "gender": "female", "height": 161.2, "weight": 51.6 }, { "gender": "female", "height": 167.5, "weight": 59 },]
console.log(data);
// 转换二维数组
var scatterData = data.map(function (item) {
return [item.height, item.weight]; // 将每个对象的height和weight作为散点坐标
});
console.log(scatterData);