flaten an object in js code example
Example 1: object flatten js
Object.assign({}, ...function _flatten(o) { return [].concat(...Object.keys(o).map(k => typeof o[k] === 'object' ? _flatten(o[k]) : ({[k]: o[k]})))}(yourObject))
Example 2: javascript flat object
function dotify(obj) {
const res = {};
function recurse(obj, current) {
for (const key in obj) {
const value = obj[key];
if(value != undefined) {
const newKey = (current ? current + '.' + key : key);
if (value && typeof value === 'object') {
recurse(value, newKey);
} else {
res[newKey] = value;
}
}
}
}
recurse(obj);
return res;
}
dotify({'a':{'b1':{'c':1},'b2':{'c':1}}})