ers, 我在使用此算法时遇到了一些麻烦。Javascript筛选器返回两个对象
我正在使用Redux,但我认为这不是真的与此问题相关。基本上这个代码中的console.log语句只返回一个对象,就像它应该那样,但函数A返回两个对象的数组(即使是没有通过函数C中的测试的对象)
I将这些功能分成三部分,看看能否帮助我解决这个问题,但我还是无法弄清楚。
有什么建议吗?
const A = (state) => {
// looks through an array and passes down a resource
return state.resources.locked.filter((resource) => {
return B(state, resource);
})
};
// looks through an array and passes down a building
const B = (state, resource) => {
return state.bonfire.allStructures.filter((building) => {
return C(building, resource);
})
};
// checks if building name and resource requirment are the same, and if building is unlocked
// then returns only that one
const C = (building, resource) => {
if (building.unlocked && building.name == resource.requires.structure) {
console.log(resource);
return resource;
}
}
请提供样品'state'。 –