Created
April 29, 2021 08:54
-
-
Save supphawit/ef9f7c8a7085927825c7bb85895b37bc to your computer and use it in GitHub Desktop.
howt to recursive to get uncountable stacked object
This file contains hidden or 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 extractor = (initialData, data) => { | |
if (data.item_group && data.item_group.length > 0) { | |
const children = data.item_group.reduce((a, b) => { | |
return [...a, ...extractor([], b)]; | |
}, []); | |
const temp = { ...data }; | |
temp.count_child = children.length; | |
delete data.item_group; | |
return [...initialData, temp, ...children]; | |
} | |
delete data.item_group; | |
data.count_child = 1; | |
return [...initialData, data]; | |
// how to use | |
// setstate(tmp.list.reduce((a, b) => [...a, ...extractor([], b)], [])); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment