Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function fetchRecursive(base_url, arr, level) {
- const result = [];
- if(level == 0) return result;
- if (Array.isArray(arr))
- for (const n of arr) {
- if (n.children) {
- let ch_arr;
- try {
- result.push({
- id: n.id,
- externalId: n.externalId,
- text: n.text,
- children: await fetch(base_url + n.id).then((r) => {
- ch_arr = { ...r };
- return r.json();
- }),
- productCount: n.productCount,
- });
- } catch (error) {
- result.push({ id: n.id, error });
- }
- result.push(...(await fetchRecursive(base_url, ch_arr, level - 1)));
- } else {
- result.push(n);
- }
- }
- return result;
- }
- данные такого вида
- {id: 13661, externalId: "КЗ000000000000002853", text: "J-Y (ST) Y", children: false, productCount: 3}
Advertisement
Add Comment
Please, Sign In to add comment