Skip to content

Commit

Permalink
Refactor nested object merge using Immutable-js
Browse files Browse the repository at this point in the history
The commit refactors the code to use the `mergeDeepWith` function of `Im
mutable-js` to keep the nested object original structure.
  • Loading branch information
Ellipse120 committed Apr 12, 2023
1 parent c7817d0 commit 5f41e60
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions note.md
Original file line number Diff line number Diff line change
Expand Up @@ -4319,3 +4319,78 @@ const refreshPage = async () => {
---
## 155. Using function `mergeDeepWith` of `Immutable-js` to keep nested object original structure.
```js
const { mergeDeepWith } = require(’immutable@4.3.0‘)

const original = {
x: {
y: 123,
z: {
a: {
b: null
}
}
},
user: {
menu: {
codes: []
},
info: {
name: null,
age: null
}
}
}

const responseData = {
x: {
y: 123,
z: null
},
user: {
menu: null,
info: {
name: ’James‘,
age: 37
}
}
}

const result = mergeDeepWith(
(oldVal, newVal) => {
if ([null, ’‘, undefined].includes(newVal)) {
return oldVal
}

return newVal
},
original,
responseData
)

console.error(result)
/**
* x: {
y: 123,
z: {
a: {
b: null
}
}
},
user: {
menu: {
codes: []
},
info: {
name: ’James‘,
age: 37
}
}
*/
```

0 comments on commit 5f41e60

Please sign in to comment.