Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save supphawit/ef9f7c8a7085927825c7bb85895b37bc to your computer and use it in GitHub Desktop.
Save supphawit/ef9f7c8a7085927825c7bb85895b37bc to your computer and use it in GitHub Desktop.
howt to recursive to get uncountable stacked object
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