gpt4 book ai didi

javascript - 从 API 数据创建对象时出现问题

转载 作者:行者123 更新时间:2023-11-30 19:33:54 25 4
gpt4 key购买 nike

我已经从 PokeAPI 获取数据,现在我正在努力创建一个包含该数据的对象。数据看起来像这样:

valueObject = {
"stats": [{
"base_stat": 45,
"effort": 0,
"stat": {
"name": "speed",
"url": "https://pokeapi.co/api/v2/stat/6/"
}
},
{
"base_stat": 65,
"effort": 0,
"stat": {
"name": "special-defense",
"url": "https://pokeapi.co/api/v2/stat/5/"
}
},
]
}

我想创建一个函数,它从所有 API 调用返回具有最大值的对象(每个 Pokemon 都有一个数据数组)。对象应如下所示:

obj = {
speed: maxvalue of base.stat,
special - defense: max of base.stat,
...
}

首先,我对每个统计数据进行了单独分配:

valueObject["speed"] = Math.max(...data.map(item => item.stats[0].base_stat));
// data is an array of all fetches

但我觉得它可以在一行或一个 for 循环中完成。不幸的是,我不能那样做,而且我总是遇到错误。我尝试映射并使用 for 循环:

// Option 1//
for (let i of data[0].stats) {
valueObject[data[0].stats[i].stat.name] = Math.max(
...data.map(item => item.stats[i].base_stat)
);
}
//Option 2 (it seems a bit off)//
valueObject[data[0].stats.map(item => item.stat.name)] = Math.max(
...data.map(item => item.stats.map(i => i.base_stat))
);

//I'm using data[0] to just get property names from any pokemon

那么,你能帮我弄清楚吗?

编辑:@mplungjan 好的,我编辑了我的帖子,但它似乎并没有运行,我也不知道为什么。

const maxStatsValues = data => {
//data array is passed as argument, but its elements look like this:
data[0] = {
"stats": [{
"base_stat": 45,
"effort": 0,
"stat": {
"name": "speed",
"url": "https://pokeapi.co/api/v2/stat/6/"
}
},
{
"base_stat": 65,
"effort": 0,
"stat": {
"name": "special-defense",
"url": "https://pokeapi.co/api/v2/stat/5/"
}
},
]
}
data[1] = {
"stats": [{
"base_stat": 72,
"effort": 0,
"stat": {
"name": "speed",
"url": "https://pokeapi.co/api/v2/stat/6/"
}
},
{
"base_stat": 90,
"effort": 0,
"stat": {
"name": "special-defense",
"url": "https://pokeapi.co/api/v2/stat/5/"
}
},
]
}
let obj = {};
data[0].stats.forEach(x => {
obj[x.stat.name] = Math.max(obj[x.stat.name] | 0, x.base_stat)
})
console.log(obj);
//my code:
// let valueObject = {};
// for (let i of data[0].stats) {
// valueObject[data[0].stats[i].stat.name] = Math.max(
// ...data.map(item => item.stats[i].base_stat)
// );
// }
// return valueObject;

return (obj);
};

最佳答案

尝试

valueObject = data.flatMap(x=>x.stats);
valueObject.forEach(x => {
obj[x.stat.name] = Math.max(obj[x.stat.name] | 0, x.base_stat)
})

let data = [];
data[0] = {
"stats": [{
"base_stat": 45,
"effort": 0,
"stat": {
"name": "speed",
"url": "https://pokeapi.co/api/v2/stat/6/"
}
},
{
"base_stat": 65,
"effort": 0,
"stat": {
"name": "special-defense",
"url": "https://pokeapi.co/api/v2/stat/5/"
}
},
]
}
data[1] = {
"stats": [{
"base_stat": 72,
"effort": 0,
"stat": {
"name": "speed",
"url": "https://pokeapi.co/api/v2/stat/6/"
}
},
{
"base_stat": 90,
"effort": 0,
"stat": {
"name": "special-defense",
"url": "https://pokeapi.co/api/v2/stat/5/"
}
},
]
}

let obj = {};
valueObject = data.flatMap(x=>x.stats);
valueObject.forEach(x => {
obj[x.stat.name] = Math.max(obj[x.stat.name] | 0, x.base_stat)
})

console.log(obj);

关于javascript - 从 API 数据创建对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56150352/

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