1
1
1nsayt2019-10-08 22:41:58
JavaScript
1nsayt, 2019-10-08 22:41:58

A function that takes an array of objects as input along with its children. How to get such an array with multilevel nesting?

There is an array of objects

const obj = [
  {
    name: 'hunter',
    isExpanded: true,
    children: [
                {
                  name: 'rain',
                  isExpanded: true,
                  children: [
                    {name: 'zevs',
                     isExpanded: false
                    },
                    {name: 'jupiter',
                     isExpanded: false
                    },
                  ]
                },
                {
                  name: 'vortex',
                  isExpanded: false
                },
                {
                  name: 'fray',
                  isExpanded: false
                },
              ]      
  },
  {
    name: 'focus',
    isExpanded: false,
    children: [
                {
                  name: 'dizik',
                  isExpanded: false
                },
                {
                  name: 'shank',
                  isExpanded: true,
                  children:[
                    {name: 'storm',
                     isExpanded: false},
                  ]
                },
                {
                  name: 'free',
                  isExpanded: false
                },
              ]      
  },
  {
    name: 'devil',
    isExpanded: true,
    children: [
                {
                  name: 'defavu',
                  isExpanded: false
                },
                {
                  name: 'hope',
                  isExpanded: true,
                  children: [
                    {name: 'rilay',
                    isExpanded: false},
                    {name: 'truth',
                    isExpanded: false}
                  ]
                },
                {
                  name: 'miracle',
                  isExpanded: false
                },
              ]      
  }
]

You need to get an array with a level 0 of objects along with all child elements, provided that the parent has the expansion flag set to true. I tried to bring this case through recursion, but there was a difficulty with the transition to the previous level. I can go to the neighboring child element (in this case, I store a link to their parent), but if I return to the parent and already consider it as a child element, it doesn’t work out. [{name:hunter,isExpanded:true},{name:'rain',isExpanded:true}, {name:"rain",isExpanded:true}, {name:"zevs", isExpanded: false},{name: "jupiter",isExpanded:false},{name:"vortex",isExpanded:false},{name:"fray",isExpanded:false},{name:"focus",isExpanded:false}, {name:"devil ",isExpanded:true},{name:"defavu",isExpanded:false}, {name:"shank",

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-10-08
@1nsayt

const flat = arr =>
  (arr || []).reduce((acc, { children, ...n }) => {
    if (n.isExpanded || !children) {
      acc.push(n, ...flat(children));
    } else {
      acc.push({ ...n, children });
    }

    return acc;
  }, []);

UPD. If nested levels need to be expanded despite isExpanded: falseone of the ancestors having a level higher than the parent, then
const flat = arr =>
  (arr || []).reduce((acc, { children, ...n }) => {
    const flatChildren = flat(children);

    if (n.isExpanded || !children) {
      acc.push(n, ...flatChildren);
    } else {
      acc.push({ ...n, children: flatChildren });
    }

    return acc;
  }, []);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question