admin管理员组文章数量:1325236
I'm trying to do an equality parison ignoring undefined values using lodash.
I'd expect the following to work:
console.log(
isEqualWith(request.body, actualRequest.body.json, (a, b) => {
console.log(a, b, a === undefined && b === undefined ? true : undefined
);
return a === undefined && b === undefined ? true : undefined;
})
);
However, the console output fails on the 'first' parison (of the whole objects), even though the customizer returns undefined. Here's the output:
{ item1: undefined, item2: 'test'} { item2: 'test' } undefined
false
It's never paring anything except the first object. I'd expect an output like:
{ item1: undefined, item2: 'test'} { item2: 'test' } undefined
undefined undefined true
'test' 'test' undefined
true
As, given the customizer returned undefined for the first check, then it would have gone through the fields of the object and performed the customzer check on those.
I'm trying to do an equality parison ignoring undefined values using lodash.
I'd expect the following to work:
console.log(
isEqualWith(request.body, actualRequest.body.json, (a, b) => {
console.log(a, b, a === undefined && b === undefined ? true : undefined
);
return a === undefined && b === undefined ? true : undefined;
})
);
However, the console output fails on the 'first' parison (of the whole objects), even though the customizer returns undefined. Here's the output:
{ item1: undefined, item2: 'test'} { item2: 'test' } undefined
false
It's never paring anything except the first object. I'd expect an output like:
{ item1: undefined, item2: 'test'} { item2: 'test' } undefined
undefined undefined true
'test' 'test' undefined
true
As, given the customizer returned undefined for the first check, then it would have gone through the fields of the object and performed the customzer check on those.
Share Improve this question asked May 12, 2019 at 11:09 JoeJoe 7,0143 gold badges56 silver badges87 bronze badges1 Answer
Reset to default 8When you return undefined
in the case of object, the method _.isEqualWith()
makes many parison to decide if the objects are equal or not. It actually checks if the number of keys is the same in both objects, which fails in your case. If it had the same number of keys, it would fail when making hasOwnProperty
checks.
To make a partial parison that ignores undefined
values, use _.isMatch()
, which actually uses the same logic but ignores the length
(and other checks) by setting the bitmask
for COMPARE_PARTIAL_FLAG
.
const o1 = { item1: undefined, item2: 'test' }
const o2 = { item2: 'test' }
const result = _.isMatch(o1, o2)
console.log(result)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.11/lodash.js"></script>
本文标签: javascriptLodash isEqualWith with customizer not checking fieldsStack Overflow
版权声明:本文标题:javascript - Lodash isEqualWith with customizer not checking fields - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742167160a2426037.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论