Skip to content

Instantly share code, notes, and snippets.

@buddalee
Created June 11, 2019 06:44
Show Gist options
  • Save buddalee/19fd99df6ca8d937b7d029001a355542 to your computer and use it in GitHub Desktop.
Save buddalee/19fd99df6ca8d937b7d029001a355542 to your computer and use it in GitHub Desktop.
flattenPages
const arr = [
{
title: '首頁',
id: 11,
parent_id: 0,
children: [
{
title: '子頁',
id: 12,
parent_id: 11,
children: []
}
]
},
{
title: '產品頁',
id: 21,
parent_id: 0,
children: []
}
]
function flattenPage (pages, newArr = []) {
pages.forEach(page => {
const _page = Object.assign({}, page)
delete _page.children
newArr.push(_page)
if (page.children.length > 0) {
flattenPage(page.children, newArr)
}
})
return newArr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment