N
N
nastya_zholudeva2020-03-04 17:50:22
JavaScript
nastya_zholudeva, 2020-03-04 17:50:22

How to do recursion in this case?

There is an array

var products = [
  {
  	name: 'pac1',
    packingLevel: 1,
    childItems: [
    	{
      	name: 'prod1',
    		packingLevel: 0,
      },
      {
      	name: 'prod2',
    		packingLevel: 0,
      }
    ]
   
  },
  
  {
  	name: 'pac2',
    packingLevel: 2,
    childItems: [
    	{
      	name: 'upac1',
    		packingLevel: 1,
        childItems: [
          {
            name: 'prod01',
            packingLevel: 0,
          },
           {
            name: 'prod02',
            packingLevel: 0,
          }
        ]
      },
      {
      	name: 'prod2',
    		packingLevel: 0,
      }
    ]
   
  },
  {
    name: 'prod11'
  },
];


it needs to be turned into
var packing = [
  {
      name: 'prod1',
      packingLevel: 1,
      childItems: [
        {
          name: 'pac1',
          packingLevel: 0,
        }
      ]

    },
    {
  	name: 'prod2',
    packingLevel: 1,
    childItems: [
    	{
      	name: 'pac1',
    		packingLevel: 0,
      }
    ]
   
  },
  {
  	name: 'prod01',
    packingLevel: 2,
    childItems: [
      {
        name: 'upac1',
        packingLevel: 1,
        childItems: [
          {
            name: 'pac2',
            packingLevel: 0,
          }
        ]
      }
    ]
   
  },
  {
  	name: 'prod02',
    packingLevel: 2,
    childItems: [
      {
        name: 'upac1',
        packingLevel: 1,
        childItems: [
          {
            name: 'pac2',
            packingLevel: 0,
          }
        ]
      }
    ]
   
  },
  {
    name: 'prod11'
  },
];

And back.
With one level of nesting, without recursion, I managed to implement , but if the nesting is more than 1 level, it doesn’t work anymore, it’s not clear where to apply recursion so that the array is built correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-03-05
@nastya_zholudeva

Helper function, very necessary, don't ask why:

const getReversedPaths = (arr, path = []) =>
  arr.reduce((acc, { childItems, ...item }) => {
    path.push(item);

    if (childItems) {
      acc.push(...getReversedPaths(childItems, path));
    } else {
      acc.push(path.length > 1
        ? path.map(({ packingLevel }, i, a) => ({ ...a[a.length - i - 1], packingLevel }))
        : [ path[0] ]
      );
    }

    path.pop();

    return acc;
  }, []);

There:
const reverseThere = arr =>
  getReversedPaths(arr).map(n =>
    n.reduceRight((child, parent) => ({ ...parent, childItems: [ child ] }))
  );

Back:
const reverseBackAgain = arr =>
  (function createTree(arr) {
    return Object.values(arr.reduce((acc, [ head, ...tail ]) => {
      if (tail.length) {
        (acc[head.name] = acc[head.name] || { ...head, childItems: [] }).childItems.push(tail);
      } else {
        acc[head.name] = head;
      }

      return acc;
    }, {})).map(n => (n.childItems && (n.childItems = createTree(n.childItems)), n));
  })(getReversedPaths(arr));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question