1 回答
TA貢獻1921條經驗 獲得超9個贊
然后刪除所有與 first 相同但僅在匯總值方面不同的元素
這不是你的代碼在做什么, res2 有
(o1.summary !== o2.summary)
這意味著如果它們不同,那么您希望包括該對象而不是排除。
只需將其更改為===,您將獲得空輸出。
重新思考過濾器的工作原理:
new_array.filter(o => diffSummary(o, first) === true)
// when an object of array will get a `true` value returned from diffSummary function then that element will be collected by filter into resulting array.
// so if res2 alongwith res and res1 is true then only this case will occur
// and your code is checking for o1.summary and o2.summary to be unequal.
// BUT, as per your expectation, you need to remove them when they are unequal. so the `true` condition need to be based on an equality comparison.
其他要點(以上評論除外)
1- 線
var new_array = array.filter(o => (JSON.stringify(o) !== JSON.stringify(first));
有語法錯誤,)在最后再 插入一個右括號
2- 遵循邏輯
if (res && res1 && res2) {
return true;
} else {
return false;
}
可以簡化為單行:
return res && res1 && res2;
添加回答
舉報
