gpt4 book ai didi

javascript - 更改数组对象的元素

转载 作者:行者123 更新时间:2023-12-02 18:26:51 24 4
gpt4 key购买 nike

首先here is a fiddle 。我想动态更改变量data的参数。如果您查看 Firebug 控制台,您可以查看数据数组对象。我查了很多资料,但找不到数据

简而言之,我想使用 return_data 元素重新生成数据数组对象。 return_data 对象的元素是动态的。在 fiddle它有 3 个元素,但可能与 3 个不同。我如何动态地更改它?

提前致谢。

var return_data = [{"name":"mrzurafa","count":315,"pop":5,"impact":7.37},{"name":"mutcato","count":20,"pop":5,"impact":56},{"name":"s0lukciyerli","count":3488,"pop":5,"impact":25.65}];

var data = google.visualization.arrayToDataTable([
['name','impact','pop','temp','count'],
['beyinsiz',80.66,1.67,2,33739900],
['mutcato',79.84,1.36,4,81902307],
['s0lukciyerli',78.6,1.84,8,5523095],
['herbkubilenadam',81.55,2.96,10,748560000],
['mrzurafa',88.09,2.05,40,307007000]
]);

最佳答案

您可以尝试一下,这会返回一个新列表,其结构与您的 return_data 完全相同:

var return_data = [{"name":"mrzurafa","count":315,"pop":5,"impact":7.37},{"name":"mutcato","count":20,"pop":5,"impact":56},{"name":"s0lukciyerli","count":3488,"pop":5,"impact":25.65}];

var data = ([
['name','impact','pop','temp','count'],
['beyinsiz',80.66,1.67,2,33739900],
['mutcato',79.84,1.36,4,81902307],
['s0lukciyerli',78.6,1.84,8,5523095],
['herbkubilenadam',81.55,2.96,10,748560000],
['mrzurafa',88.09,2.05,40,307007000]
]);

//prepare object
var item = {}
for (j=0;j<data[0].length;j++) {
item[data[0][j]] = "";
}

//prepare list of objecs
var objs = []

for (i=1;i< data.length;i++) {
item['name'] = data[i][0];
item['impact']=data[i][1];
item['pop']=data[i][2];
item['temp']=data[i][3];
item['count']=data[i][4];
objs.push(item)
}

console.log(objs)

希望它能有所帮助。

关于javascript - 更改数组对象的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18286988/

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