Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LispyAriaro/fa731ca560f9f6d562e9e7ee1d3ea973 to your computer and use it in GitHub Desktop.
Save LispyAriaro/fa731ca560f9f6d562e9e7ee1d3ea973 to your computer and use it in GitHub Desktop.
Clean object for firestore recursively
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