[英]How to add an Array to an Javascript Object
I have the following code: 我有以下代码:
var optionsChart = {
data: [{
type: "bla",
dataPoints: [{
x: 1,
y: 3
}, {
x: 1,
y: 2
}, {
x: 3,
y: 4
}]
},
etc...
}
Now I tried to generate the Datas in the dataPoints field dynamically. 现在,我尝试在dataPoints字段中动态生成数据。 I have an Array like this:
我有一个这样的数组:
dataArray = [[1,3], [1,2],[3,4]];
I try to insert his after dataPoints , but when doing this it needs to be exactly like in the code above, but I don't know how to do that. 我尝试在dataPoints之后插入他,但是这样做时需要与上面的代码完全一样,但是我不知道该怎么做。
Any suggestions here? 有什么建议吗? Thank you very much!
非常感谢你!
You need to do this way: 您需要这样做:
optionsChart.data[0].dataPoints.push(dataArray.map(function (obj) {
return {
x: obj[0],
y: obj[1]
};
}));
Or try this way: 或尝试这种方式:
optionsChart.data[0].dataPoints = dataArray.map(function (obj) {
return {
x: obj[0],
y: obj[1]
};
});
Here you go : 干得好 :
optionsChart.data[0].dataPoints.push(dataArray.map(function(t) {
return {x: t[0], y: t[1]};
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.