如何访问data1数组中y“column”中的数据并将数据推送到新的数组data2?
var data1 = [
{x:1, y:3, s:0.15},
{x:2, y:2.7, s:0.1},
{x:0.7, y:3, s:0.15},
{x:2.3, y:2.9, s:0.12}
];
var data2 = [3, 2.7, 3, 2.9]
这就是我的尝试:
var data2 = [];
for (var j = 0; j < data1.length; j++) {
if ( ) {
data2.push( );
};
};
我不是javascript用户。感谢是否有人帮助完成它!
答案 0 :(得分:1)
您可以像这样使用.map
var data1 = [
{x: 1, y: 3, s: 0.15},
{x: 2, y: 2.7, s: 0.1},
{x: 0.7, y: 3, s: 0.15},
{x: 2.3, y: 2.9, s: 0.12}
];
var data2 = data1.map(function (e) {
return e.y;
})
console.log(data2);
或修改您的版本
var data1 = [
{x: 1, y: 3, s: 0.15},
{x: 2, y: 2.7, s: 0.1},
{x: 0.7, y: 3, s: 0.15},
{x: 2.3, y: 2.9, s: 0.12}
];
var data2 = [];
for (var j = 0; j < data1.length; j++) {
data2.push(data1[j].y);
};
console.log(data2);