Dealing with list

clipboard.png

how can data with level 2 be more easily classified under level 1 and category = parent_category?
there is a children: [] field under the data with a level of 1.
Thank you

Dec.23,2021

var arr=[
    {cate:'help',level:'1',par:''},
    {cate:'about',level:'1',par:''},
    {cate:'disc',level:'1',par:''},
    {cate:'dis_ins',level:'2',par:'disc'},
    {cate:'beian',level:'2',par:'disc'},
    {cate:'test',level:'2',par:'help'},
    {cate:'test_123',level:'2',par:'about'}
]

repl(arr);
function repl(arr){
  var cach = [],newArr=[];
  for(var i=0;i<arr.length;iPP){
    var cate = arr[i].cate,
        par = arr[i].par;
    if(arr[i].level=='1'){
      arr[i].children=[];
      newArr.push(arr[i]);
      cach.push(cate);
    }else{
      var idx=cach.indexOf(par);
      if(idx!=-1){
        newArr[idx].children.push(arr[i]);
      }
    }
  }

if there are only two levels, use the following

function classify(arr, level = '1', parent_category = '') {
  const children = arr.filter(
    a => a.level === `${level}` && a.parent_category === parent_category,
  )
  return children.length === 0
    ? children
    : children.map(a => ({
        ...a,
        children: classify(arr, parseInt(a.level, 10) + 1, a.parent),
      }))
}
const result = classify(arr)
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b33007-2b3a7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b33007-2b3a7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?