我很难理解为什么这段代码不起作用。不过,它可以处理一个小数据集。
const gl = wList.GreateLeague
const data = JSON.parse(fs.readFileSync("./raw/data.json"))
(function () {
let glFinds = data.pokemons.filter((poke) => gl.hasOwnProperty(poke.pokemon_id))
let ax = glFinds.filter((p) => {
return gl[p.pokemon_id].stats.filter((ammo) => {
return (
p.attack === ammo[0] && p.defence === ammo[1] && p.stamina === ammo[2]
);
});
});
console.log(glFinds)
console.log(ax)
})();
我对两个控制台的输出是相同的,因此ax上的过滤器不能工作。如果我包括testData.json
,ax工作得很好,它比data.json
小。
编辑:统计数据看起来像这样。在这里,1,2是进攻防守和耐力。
"stats": [
[1, 15, 15, 20.5, 1494],
[0, 13, 11, 20.5, 1494],
[2, 14, 15, 20.5, 1494],
[0, 10, 15, 20.5, 1494],
[1, 12, 11, 20.5, 1494],
[0, 15, 15, 20.5, 1494],
[3, 13, 15, 20.5, 1494],
[0, 14, 10, 20.5, 1494],
[2, 15, 14, 20.5, 1494],
[0, 11, 13, 20.5, 1494]
],
我的数据文件有如下所示的对象数组:
{
"pokemon_id": 155,
"attack": 2,
"defence": 5,
"stamina": 0,
"move1": 209,
"move2": 125,
"level": 4,
}
我试图过滤任何对象匹配(0,1,2索引)的统计数据数组,对象的攻击,防御和耐力必须匹配任何一个统计数据数组前3元素。
发布于 2022-09-17 11:58:58
filter
中的函数必须返回true
或false
,这取决于当前项是否包含在输出数组中。但是您的函数返回gl[p.pokemon_id].stats.filter(...)
,这是一个数组,正如Andreas已经指出的那样。
当试图将数组计算为true
或false
时,它可以算作true
(即使它是空的),因此您的整个filter
过程实际上什么也不做。
if ([]) console.log(true);
// true
https://stackoverflow.com/questions/73754570
复制相似问题