Home > other >  how to create from object and nested objects one array
how to create from object and nested objects one array

Time:11-03

I have this array of objects with nested objects "children".. the number of nested children arrays that can be is not defined

let a = [
    { id: 0, title: 'a', children: [ { id: 1, title: 'aa', children: [ { id: 2, title: 'aaa', children: []} ]}] },
    { id: 3, title: 'b', children: [ { id: 4, title: 'bb', children: []}] },
    { id: 5, title: 'c', children: [] },
    { id: 6, title: 'd', children: [ { id: 7, title: 'dd', children: [ { id: 8, title: 'ddd', children: []} ]}] },
]

and I need foreach them, take to the array.. with level of nested:

let b = [
    { id: 0, title: 'a', level: 0 },
    { id: 1, title: 'aa', level: 1 },
    { id: 2, title: 'aaa', level: 2 },
    { id: 3, title: 'b', level: 0 },
    { id: 4, title: 'bb', level: 1 },
    { id: 5, title: 'c', level: 0 },
    { id: 6, title: 'd', level: 0 },
    { id: 7, title: 'dd', level: 1 },
    { id: 8, title: 'ddd', level: 2 },
]

I tired recursively code, but its not working.. thank for help

CodePudding user response:

Here is a recursive function called makeLevels that outputs your result.

let a = [
    { id: 0, title: 'a', children: [{ id: 1, title: 'aa', children: [{ id: 2, title: 'aaa', children: [] }] }] },
    { id: 3, title: 'b', children: [{ id: 4, title: 'bb', children: [] }] },
    { id: 5, title: 'c', children: [] },
    { id: 6, title: 'd', children: [{ id: 7, title: 'dd', children: [{ id: 8, title: 'ddd', children: [] }] }] },
];


function makeLevels(entry, result = [], level = 0) {
    for (let i = 0, len = entry.length; i < len; i  ) {
        const item = entry[i];
        result.push({ id: item.id, title: item.title, level });
        if (item.children?.length) {
            makeLevels(item.children, result, level   1);
        }
    }

    return result;
}

console.log(makeLevels(a));

Output:

[
    { "id": 0, "title": "a", "level": 0 },
    { "id": 1, "title": "aa", "level": 1 },
    { "id": 2, "title": "aaa", "level": 2 },
    { "id": 3, "title": "b", "level": 0 },
    { "id": 4, "title": "bb", "level": 1 },
    { "id": 5, "title": "c", "level": 0 },
    { "id": 6, "title": "d", "level": 0 },
    { "id": 7, "title": "dd", "level": 1 },
    { "id": 8, "title": "ddd", "level": 2 }
]

CodePudding user response:

You can try this approach:

let a = [{ id: 0, title: 'a', children: [ { id: 1, title: 'aa', children: [ { id: 2, title: 'aaa', children: []} ]}] }, { id: 3, title: 'b', children: [ { id: 4, title: 'bb', children: []}] }, { id: 5, title: 'c', children: [] }, { id: 6, title: 'd', children: [ { id: 7, title: 'dd', children: [ { id: 8, title: 'ddd', children: []} ]}] },]

function flattenArray(arr, index = 0) {
  return arr.reduce((acc, {children, ...rest}) => [
    ...acc, 
    {...rest, level: index},
    ...flattenArray(children, index 1)
  ], 
  [])
}

console.log(flattenArray(a))

CodePudding user response:

To make it a little more readable, you could also do it like this.

let a = [{ id: 0, title: 'a', children: [ { id: 1, title: 'aa', children: [ { id: 2, title: 'aaa', children: []} ]}] }, { id: 3, title: 'b', children: [ { id: 4, title: 'bb', children: []}] }, { id: 5, title: 'c', children: [] }, { id: 6, title: 'd', children: [ { id: 7, title: 'dd', children: [ { id: 8, title: 'ddd', children: []} ]}] },]

function levels(obj, level = 0, arr = []) {
  for (const { id, title, children } of obj) {
    arr.push({ id, title, level });
    if (Array.isArray(children)) levels(children, level   1, arr);
  }

  return arr;
}

console.log(levels(a))

  • Related