Tag and build

This commit is contained in:
Angelos Chalaris
2017-12-16 14:24:50 +02:00
parent 1ffe150990
commit 4fc9b6904a
3 changed files with 29 additions and 3 deletions

View File

@ -91,6 +91,7 @@
* [Write JSON to file](#write-json-to-file) * [Write JSON to file](#write-json-to-file)
### Object ### Object
* [Clean JSON object](#clean-json-object)
* [Object from key value pairs](#object-from-key-value-pairs) * [Object from key value pairs](#object-from-key-value-pairs)
* [Object to key value pairs](#object-to-key-value-pairs) * [Object to key value pairs](#object-to-key-value-pairs)
* [Shallow clone object](#shallow-clone-object) * [Shallow clone object](#shallow-clone-object)
@ -1032,6 +1033,30 @@ const jsonToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stri
[⬆ back to top](#table-of-contents) [⬆ back to top](#table-of-contents)
## Object ## Object
### Clean JSON object
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.
```js
const cleanObj = (obj, keysToKeep = [], childIndicator) => {
Object.keys(obj).forEach(key => {
if (key === childIndicator) {
cleanObj(obj[key], keysToKeep, childIndicator);
} else if (!keysToKeep.includes(key)) {
delete obj[key];
}
})
}
/*
testObj = {a: 1, b: 2, children: {a: 1, b: 2}}
cleanObj(testObj, ["a"],"children")
console.log(testObj)// { a: 1, children : { a: 1}}
*/
```
[⬆ back to top](#table-of-contents)
### Object from key-value pairs ### Object from key-value pairs
Use `Array.reduce()` to create and combine key-value pairs. Use `Array.reduce()` to create and combine key-value pairs.

View File

@ -1,4 +1,4 @@
### Clean Json objects ### Clean JSON object
Use `Object.keys()` method to loop over 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.
@ -7,9 +7,9 @@ also if you give it a special key(`childIndicator`) it will search deeply inside
const cleanObj = (obj, keysToKeep = [], 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], keysToKeep, childIndicator) cleanObj(obj[key], keysToKeep, childIndicator);
} else if (!keysToKeep.includes(key)) { } else if (!keysToKeep.includes(key)) {
delete obj[key] delete obj[key];
} }
}) })
} }

View File

@ -16,6 +16,7 @@ capitalize-first-letter:string
chain-asynchronous-functions:function chain-asynchronous-functions:function
check-for-palindrome:string check-for-palindrome:string
chunk-array:array chunk-array:array
clean-JSON-object:object
collatz-algorithm:math collatz-algorithm:math
compact:array compact:array
compose-functions:function compose-functions:function