Ask for advice on how to write a js recursive function.

work needs to use a js recursive writing, really can not write it, ask for help, you can add Wechat to send a big red packet! The data structure of trustworthiness ~
is as follows:

[
      {
        key: "1",
        name: "name",
        label: "",
        items: [
          {
            name: "name",
            key: "1-1",
            label: "",
            items: [
              {
                name: "name",
                key: "1-1-1",
                label: "-0",
                items: [
                  {
                    name: "name",
                    key: "1-1-1-1",
                    label: "-0-0",
                    items: "",
                  },
                  {
                    name: "name",
                    key: "1-1-1-2",
                    label: "-0-1",
                    items: "",
                  },
                ],
              },
              {
                name: "name",
                key: "1-1-2",
                label: "-1",
                items: "",
              },
            ],
          },
          {
            name: "name",
            key: "1-2",
            label: "",
            items: [
              {
                name: "name",
                key: "1-2-1",
                label: "1",
                items: "",
              },
              {
                name: "name",
                key: "1-2-2",
                label: "2",
                items: "",
              },
            ],
          },
          {
            name: "name",
            key: "1-3",
            label: "",
            items: [
              {
                name: "name",
                key: "1-3-1",
                label: "1",
                items: "",
              },
              {
                name: "name",
                key: "1-3-2",
                label: "2",
                items: "",
              },
            ],
          },
        ],
      },
    ];

there may be countless levels of recursion. The requirement is that the "key" field in the data itself is not available, it should be generated in a loop, and the format of n-n-n should be generated hierarchically. So, a recursive function that passes in such a set of data returns data with the same structure and content, but with key with n-n-n.

Mar.12,2021

function f(datas, keys) {
  datas.forEach((item, i) => {
    keys.push(i + 1)
    item.key = keys.join('-')
    if (item.items) {
      f(item.items, keys)
    }
    keys.pop()
  })
  return datas
}

console.log(f([
      {
        name: 'name',
        label: '',
        items: [
          {
            name: 'name',
            label: '',
            items: [
              {
                name: 'name',
                label: '-0',
                items: [
                  {
                    name: 'name',
                    label: '-0-0',
                    items: '',
                  },
                  {
                    name: 'name',
                    label: '-0-1',
                    items: '',
                  },
                ],
              },
              {
                name: 'name',
                label: '-1',
                items: '',
              },
            ],
          },
          {
            name: 'name',
            label: '',
            items: [
              {
                name: 'name',
                label: '1',
                items: '',
              },
              {
                name: 'name',
                label: '2',
                items: '',
              },
            ],
          },
          {
            name: 'name',
            label: '',
            items: [
              {
                name: 'name',
                label: '1',
                items: '',
              },
              {
                name: 'name',
                label: '2',
                items: '',
              },
            ],
          },
        ],
      },
    ], []))

filters(option,rs){
  option.forEach((r,i)=>{
    r.keys=rs.keys ? rs.keys+'-'+Number(i+1) : Number(i+1)+'';
    if(r['items']){
      this.filters(r.items,r)
    }
  })
  return option
},
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-1b3838d-40683.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-1b3838d-40683.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?