Created
May 5, 2018 03:39
-
-
Save LispyAriaro/fa731ca560f9f6d562e9e7ee1d3ea973 to your computer and use it in GitHub Desktop.
Clean object for firestore recursively
This file contains 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
let clean = (obj) => { | |
let dateCounter = 0; | |
function traverse(parent, key) { | |
if (_.isDate(parent[key])) { | |
parent[key] = parent[key].getTime(); //convert to timestamp | |
dateCounter++; | |
} | |
if (_.isArray(parent[key])) { | |
_.each(parent[key], function (_ob) { | |
_.each(Object.keys(_ob), function (key) { | |
traverse(_ob, key) | |
}); | |
}) | |
} | |
if (parent[key] === undefined){ | |
delete parent[key] | |
} else if (parent[key] === null){ | |
delete parent[key] | |
} else if (typeof parent[key] === "object") { | |
let obj = parent[key] | |
let objKeys = Object.keys(obj) | |
if(objKeys.length > 0) { | |
_.each(objKeys, function (objKey) { | |
traverse(obj, objKey) | |
}) | |
} | |
} | |
if (typeof parent[key] === "function") { | |
delete parent[key] | |
} | |
} | |
_.each(Object.keys(obj), function (key) { | |
traverse(obj, key) | |
}); | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment