gpt4 book ai didi

javascript 键和对象

转载 作者:行者123 更新时间:2023-11-28 02:30:14 25 4
gpt4 key购买 nike

我有下面的 JavaScript 代码:

function init() {
var data_pie = [];
var data_key = [];

data_pie.push(10,12,30,40,80,25);
data_key.push("x1","x2","x3","x4","x5","x6");
g.update(data_pie, data_key);
}

update: function(data, key) {
var i=-1;

var streakerDataAdded = d3.range(data.length).map(function() {
i++;
return {
name: key[i],
totalPlayers: data[i]
}
});
}

如何优化我的代码以使用此对象:

var data='{"data":[{"x1":"10","x2":"12","x3":"30","x4":"40","x5":"80","x6":"25"}]}';

而是 data_piedata_key 数组?

最佳答案

如果无法对此进行测试(无法访问其余代码),您可以尝试以下操作:

function init() {
var data='{"data":[{"x1":"10","x2":"12","x3":"30","x4":"40","x5":"80","x6":"25"}]}';

for(key in data.data[0]){
g.update(data.data[0][key], key, data.data[0].length);
}
}

update: function(data, key, length) {
var streakerDataAdded = d3.range(length).map(function() {
return {
name: key,
totalPlayers: data
}
});
}

关于javascript 键和对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14332685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com