object flatten js
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}}}) //{'a.b1.c':1,'a.b2.c':1}Object.assign(
{},
...function _flatten(o) {
return [].concat(...Object.keys(o)
.map(k =>
typeof o[k] === 'object' ?
_flatten(o[k]) :
({[k]: o[k]})
)
);
}(yourObject)
)
Object.assign({}, ...function _flatten(o) { return [].concat(...Object.keys(o).map(k => typeof o[k] === 'object' ? _flatten(o[k]) : ({[k]: o[k]})))}(yourObject))
Also in JavaScript:
- javascript code for year in html
- routes in node js
- express get host url
- jquery selected option
- javascript regex match character from a set of characters
- javascript write to file and download
- nextjs router get complete url
- typescript vs javascript
- js show span for 5 seconds
- jquery remove array element by key
- get contents between tags javascript
- call js script in html
- jquery copier dans le presse papier
- new map js
- javascript circular evaluation
- asjasfawepiowjpowlklkcdlkdLkdlkskskjskknisbsbu
- jquery $(document.on click
- javascript dynamic variable name
- javascript find textarea
- javascript add object to array
- how to refresh a web page automatically in javascript
- get all entries in object as array hjs
- javascript localstorage
- javascript associative array