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;
添加回答
举报