Skip to content

Instantly share code, notes, and snippets.

@sabha
Created November 10, 2017 22:48
Show Gist options
  • Save sabha/b2bb02bffdf90f0cf3c57c9c442e43fe to your computer and use it in GitHub Desktop.
Save sabha/b2bb02bffdf90f0cf3c57c9c442e43fe to your computer and use it in GitHub Desktop.
Recursion React Children
var childrens = [{name: 'ql-0', childrens : [ { name: 'ql-0-0' }] },{name: 'ql-1'}, {name: 'ql-2', childrens : [ { name: 'ql-2-0' }, { name: 'ql-2-1' }] }]
function recursive(children, i){
return children.map((child, index) => {
if(child.childrens) return Object.assign({},child,{index: `${i}-${index}`, childrens: recursive(child.childrens, `${i}-${index}`)});
else return Object.assign({},child,{index: `${i}-${index}`});
})
}
var a = recursive(childrens,'ql');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment