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'。 –