add a functions that deeply cleans json objects from unwanted keys
This commit is contained in:
20
snippets/json-cleaner.md
Normal file
20
snippets/json-cleaner.md
Normal file
@ -0,0 +1,20 @@
|
||||
### Clean Json objects
|
||||
|
||||
Clean your json object from unwanted keys, deeply.
|
||||
provide set of `keys` to keep and an indicator for `children` if there is any.
|
||||
|
||||
```js
|
||||
const cleanObj = (obj, keys = [], childIndicator) => {
|
||||
Object.keys(obj).forEach(key => {
|
||||
if (key === childIndicator) {
|
||||
cleanObj(obj[key], keys, childIndicator)
|
||||
} else if (!keys.includes(key)) {
|
||||
delete obj[key]
|
||||
}
|
||||
})
|
||||
}
|
||||
/*
|
||||
dirtyObj = { a: 1, b: 2, children: {a: 1, b :2}}
|
||||
let cleaned = cleanObj(dirtyObj, [a]) // { a: 1, children : { a: 1}}
|
||||
*/
|
||||
```
|
||||
Reference in New Issue
Block a user