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 badges
Add a ment  | 

1 Answer 1

Reset to default 8

When 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