gpt4 book ai didi

javascript - 如何从此对象列表中提取 "name"和 "runs"

转载 作者:行者123 更新时间:2023-11-28 05:57:27 24 4
gpt4 key购买 nike

这是我制作的功能。我想提取数组中的名称以及各个团队的运行,以便我可以将它们用作响应以在图表中显示它们。

function unpack(obj, key) {
Object.keys(obj).forEach(function(key) {
var collection = [];
var collections = [];
for (var k in obj) {
collection.push(obj[k]);
}
console.log(collection);
return collection;
});
}

这是对象列表

[
{
name: "Scotland",
stats: {
runs: 16135,
wickets_taken: 1,
matches_played: 354
}
},
{
name: "Pakistan",
stats: {
runs: 70900,
wickets_taken: 43,
matches_played: 1066
}
},
{
name: "West Indies",
stats: {
runs: 61300,
wickets_taken: 280,
matches_played: 739
}
},
{
name: "Sri Lanka",
stats: {
runs: 108242,
wickets_taken: 741,
matches_played: 1049
}
}]

最佳答案

设存储对象的变量为obj

function getData() {
var names = [];
var runs = [];
obj.forEach(function(item) {
names.push(item.name);
runs.push(item.stats.runs);
});
return {
runs: runs,
names: names,
}
}
//access them outside as:
var data = getData();
var names = data.names; //accessing name and run array returned from fxn
var runs = data.runs;

关于javascript - 如何从此对象列表中提取 "name"和 "runs",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37537699/

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