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

A recursive word cannot be written in the work. Ask the master for help and accept a reward. Thank you. Thanks Thanks ()
has the following data

.
[{
  id: "1",
  name: "1",
  items: [
    {
      id: "2",
      name: "2",
      items: [
        {
          id: "3",
          name: "3",
          items: [
            {
              id: "4",
              name: "4",
            },
          ],
        },
      ],
    },
    {
      id: "5",
      name: "5",
    },
    {
      id: "6",
      name: "6",
    },
  ],
},
{
  id: "7",
  name: "7",
}];

requires that there be a function, pass in an id value, and find out from the data the object whose id is passed in id, and all the parent objects of this object

for example, if the input id is" 5percent, then

is returned.
[{id: "5", name: "5"}, {id: "1", name: "1"}]

input id is" 3percent, return

[{id: "3", name: "3"}, {id: "2", name: "2"}, {id: "1", name: "1"}]

Thank you for your help ~

Mar.14,2021

let root = {
  id: '1',
  name: '1',
  items: [
    {
      id: '2',
      name: '2',
      items: [
        {
          id: '3',
          name: '3',
          items: [
            {
              id: '4',
              name: '4',
            },
          ],
        },
      ],
    },
    {
      id: '5',
      name: '5',
    },
    {
      id: '6',
      name: '6',
    },
  ],
};

function search(root, id) {
  if(root.id == id) return [ { id: id, name: root.name} ];
  else {
    for(let item of (root.items || [])) {
      let res = search(item, id);
      if(res.length) return [ ...res, { id: root.id, name: root.name } ];
    }
    return [];
  }
}

console.log(search(root, '5'));
console.log(search(root, '3'));
The idea of

is similar to "print the string entered by the user in reverse order using recursion". Each layer recursively returns the path searched by that layer, and the empty array represents the search failure; when the layer recursively gets the recursive result returned by the lower layer, the current node is added to the path and then returned to the upper layer.

if the search tree is particularly large, it is best to optimize the return method. I write this way to generate a new array object every time, and you can pass the same path with Array.push .


let root = [{
  id: '1',
  name: '1',
  items: [
    {
      id: '2',
      name: '2',
      items: [
        {
          id: '3',
          name: '3',
          items: [
            {
              id: '4',
              name: '4',
            },
          ],
        },
      ],
    },
    {
      id: '5',
      name: '5',
    },
    {
      id: '6',
      name: '6',
    },
  ],
}];

function search(root, id) {
  for (let i = 0; i < root.length; i += 1) {
    if (root[i].id === id) {
      return [{ id, name: root[i].name }];
    }
  }
  for (let i = 0; i < root.length; i += 1) {
    if (root[i].items && Array.isArray(root[i].items)) {
      const res = search(root[i].items, id);
      if (res.length) return [...res, { id: root[i].id, name: root[i].name }];
    }
  }
}

console.log(search(root, '5'));
console.log(search(root, '3'));
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-1e4db7b-44f4a.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-1e4db7b-44f4a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?