Last active
December 3, 2019 03:11
-
-
Save jebai0521/a7ffc87c8902b82baf34cdf66235456d to your computer and use it in GitHub Desktop.
Compare JS Array Use Lodash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const _ = require('lodash'); | |
| // compare object , same key and value, different key sort | |
| function case1 () { | |
| const a = { k1: 'v1', k2: { k21: 'v21' } }; | |
| const b = { k2: { k21: 'v21' }, k1: 'v1' }; | |
| console.log(_.isEqual(a, b)); | |
| // output | |
| // true | |
| } | |
| // compare object, same key, different value | |
| function case2 () { | |
| const a = { k1: 'v1', k2: { k21: 'v22' } }; | |
| const b = { k1: 'v1', k2: { k21: 'v21' } }; | |
| console.log(_.isEqual(a, b)); | |
| // output | |
| // false | |
| } | |
| // compare array, same element value, same sort | |
| function case3 () { | |
| const a = { k1: 'v1', k2: { k21: 'v22' } }; | |
| const b = { k1: 'v1', k2: { k21: 'v21' } }; | |
| console.log(_.isEqual([a, b], [a, b])); | |
| // output | |
| // true | |
| } | |
| // compare array, same element value, different sort | |
| function case4 () { | |
| const a = { k1: 'v1', k2: { k21: 'v22' } }; | |
| const b = { k1: 'v1', k2: { k21: 'v21' } }; | |
| console.log(_.isEqual([a, b], [b, a])); | |
| // output | |
| // false | |
| } | |
| // compare array, different element | |
| function case5 () { | |
| const a = { k1: 'v1', k2: { k21: 'v22' } }; | |
| const b = { k1: 'v1', k2: { k21: 'v21' } }; | |
| const result = _.isEmpty(_.differenceWith([a], [b], _.isEqual)); | |
| console.log(result); | |
| // output | |
| // false | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment