My code from: https://gist.github.com/penguinboy/762197
let isPlainObj = (o) => Object.prototype.toString.call(o) == "[object Object]"
// this ^ is enough. If you check prototypes – you're doing it wrong. If somethings pretends to be plain – we have to accept that
// e.g. this: https://github.com/jonschlinkert/is-plain-object is an entirely WRONG approach
let flattenObj = (obj, keys=[]) => {
return Object.keys(obj).reduce((acc, key) => {
return Object.assign(acc, isPlainObj(obj[key])
? flattenObj(obj[key], keys.concat(key))
: {[keys.concat(key).join(".")]: obj[key]}
)
}, {})
}
console.log(flattenObj({}) // {}
console.log(flattenObj({foo: "foo"}) // {foo: "foo"}
console.log(flattenObj({
users: {
set: "whatnot",
},
projects: {
set: "whatnot",
},
dates: {
d1: new Date(),
d2: new Date(),
},
array: [{foo: "foo"}, {bar: "bar"}]
}))
{ 'users.set': 'whatnot',
'projects.set': 'whatnot',
'dates.d1': <dateobj1>,
'dates.d2': <dateobj2>,
array: [ { foo: 'foo' }, { bar: 'bar' } ] }