Created
June 11, 2019 06:44
-
-
Save buddalee/19fd99df6ca8d937b7d029001a355542 to your computer and use it in GitHub Desktop.
flattenPages
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 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