Created
November 4, 2019 21:10
-
-
Save vladimir-ivanov/0792327d9ff91f00e1cb40612a1b2d34 to your computer and use it in GitHub Desktop.
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
const result = []; | |
const traverse = (obj, path = '', parentRow = {}) => { | |
let row = { ...parentRow }; | |
Object.entries(obj).forEach(([key, val]) => { | |
const currentPath = path ? `${path}.${key}` : key; | |
if (Array.isArray(val) && val.length > 0) { | |
val.forEach(el => { | |
traverse(el, currentPath, row); | |
}); | |
} else if (typeof val === 'object' && !(val instanceof Array)) { | |
traverse(val, currentPath, row); | |
} else { | |
row = { ...row, [currentPath]: val } | |
} | |
}); | |
result.push(row); | |
result.forEach(r => { | |
const newRow = ({ ...r, ...row }); | |
if (!result.find(l => JSON.stringify(l) === JSON.stringify(newRow))) { | |
result.push(newRow); | |
} | |
}); | |
}; | |
traverse({ | |
"top": { "root": "vlads" }, | |
"car": "mt car", | |
"madCow": [ | |
{ | |
"bottom": "fdsafa", | |
"bad": 'bad rules once' | |
}, | |
{ | |
"bottom": "fdsfdsf", | |
"bad": 'bad rules twice' | |
}, | |
{ | |
"bottom": "fdsfasfsa", | |
"bad": 'bad rules twice' | |
}, | |
{ | |
"bottom": "fsfdsfdsfs", | |
"bad": 'bad rules twice' | |
} | |
], | |
"interior": [ | |
{ | |
"bottom": "CCCCccccccccc", | |
"bad": 'bad rules once' | |
}, | |
{ | |
"bottom": "DDDDDDD", | |
"bad": 'bad rules twice' | |
}, | |
{ | |
"bottom": "DDDDddddddDDD", | |
"bad": 'bad rules twice' | |
}, | |
{ | |
"bottom": "fdsfsfs", | |
"bad": 'bad rules twice' | |
} | |
], | |
"exterior": [ | |
{ | |
"bottom": "CCCCdere", | |
"bad": 'bad rules once' | |
}, | |
{ | |
"bottom": "DDDDrerereDDD", | |
"bad": 'bad rules fddfdfd' | |
}, | |
{ | |
"bottom": "DDDDDDD", | |
"bad": 'bad rules mmmm' | |
} | |
] | |
}); | |
console.warn(result.filter(r => Object.keys(r).length > 6)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment