refactoring grammatical problems and assigning better name to keys argument.

This commit is contained in:
iamsoorena
2017-12-15 20:23:23 +03:30
parent f7123086f7
commit e642274382

View File

@ -1,14 +1,14 @@
### Clean Json objects ### Clean Json objects
Use `Object.keys()` method to iter through given json object and deleting keys that are not `include`d in given array. Use `Object.keys()` method to loop over given json object and deleting keys that are not `include`d in given array.
also if you give it a special key(`childIndicator`) it will search deeply inside it to apply function to inner objects too. also if you give it a special key(`childIndicator`) it will search deeply inside it to apply function to inner objects too.
```js ```js
const cleanObj = (obj, keys = [], childIndicator) => { const cleanObj = (obj, keysToKeep = [], childIndicator) => {
Object.keys(obj).forEach(key => { Object.keys(obj).forEach(key => {
if (key === childIndicator) { if (key === childIndicator) {
cleanObj(obj[key], keys, childIndicator) cleanObj(obj[key], keysToKeep, childIndicator)
} else if (!keys.includes(key)) { } else if (!keysToKeep.includes(key)) {
delete obj[key] delete obj[key]
} }
}) })