How to change the recursive algorithm of js multi-layer nested data into iteration

because the data parameters given by the background are different from those needed by the front end, the data given by the background is as follows (with multiple layers of nesting):

code: null
id: 1
name: ""
parentId: null
sort: null
type: null
child: [{
    code: "eos-admin-store-control"
    id: 3
    name: "eos"
    parentId: 1
    sort: null
    type: null,
     child: [{
        code: "eos-admin-store-list"
        id: 4
        name: "eos"
        parentId: 3
        sort: null
        type: null,
        child: []
     },
     {
        code: "eos-admin-store-list"
        id: 5
        name: "eos11111111"
        parentId: 3
        sort: null
        type: null,
        child: []
     }
     ]
}]

how to replace it iteratively with recursion? Because recursion affects performance.

traverse the corresponding parameters according to the plug-in requirements. The format of the parameters is as follows:

fromData:[
        {
          id: "1",
          pid: 0,
          label: " 1",
          children: [
            {
              id: "1-1",
              pid: "1",
              label: " 1-1",
              children: []
            },
            {
              id: "1-2",
              pid: "1",
              label: " 1-2",
              children: [
                {
                  id: "1-2-1",
                  pid: "1-2",
                  children: [],
                  label: " 1-2-1"
                },
                {
                  id: "1-2-2",
                  pid: "1-2",
                  children: [],
                  label: " 1-2-2"
                }
              ]
            },

            {
              id: "2-1",
              pid: "1",
              label: " 1-2",
              children: [
                {
                  id: "2-2-1",
                  pid: "2-1",
                  children: [],
                  label: " 1-2-1"
                },
                {
                  id: "2-2-2",
                  pid: "2-1",
                  children: [],
                  label: " 1-2-2"
                }
              ]
            }
          ]
        },
      ]
Apr.01,2022

is it most appropriate to use recursion in such an uncertain level scenario? this kind of data structure transformation is only to get the data conversion once, how much computation can it have, ah, and you begin to worry about the impact of recursion on performance ? If you want to pursue performance, you should change the format of the backend

. < hr >
const createData = ({level, size, _level, pid}) => {
  const result = [];
  _level = _level || 0;
  size = size || 1;
  for (let i = 0; i < size; iPP) {
    const id = `${_level}-${i}`;
    result.push({
      id,
      parentId: pid || '',
      name: `${_level} ${i}`,
      child: _level < level ? createData({level, size, _level: _level + 1, pid: id}) : [],
    });
  }
  return result;
};

const recursionChange = data => data.map(item => ({
  id: item.id,
  pid: item.parentId,
  label: item.name,
  children: recursionChange(item.child),
}));

console.time('create');
const data = createData({level: 10, size: 3});
console.timeEnd('create');

console.time('recursion');
const changeData = recursionChange(data);
console.timeEnd('recursion');

console.log(data, changeData);

just now I couldn't help but want to test what the subject said recursion affects performance whether there is writing the above test code, the object of 3 to the power of 10 + is big enough and deep enough, 37ms is recursively converted, and there is no performance problem at all

.

Yes, but not necessary. If you have to use it, then use while. A result is maintained on the outside, and it is called all the time inside. Break can also be used when the completion condition is met. Ps recursion affects performance mostly because you use recursive posture incorrectly. Learn about tail recursion

.
Menu