Search for and display items of objects nested with JavaScript

1

I have two lists, the aggregation brings me the categories codes and the categories list brings all the categories that I have registered. I need to look up the categories in categories using the aggregation ids to create a new list only with this filter. In the case of categories that are daughters (children property) I need to put in this new list the parent category and the daughter category.

Example, in the case of id 22 (rubber hammer) I would have to display not only the rubber hammer but also your parents: tools and hammer.

var aggregation = [
        {id: 1},
        {id: 12},
        {id: 22}        
      ]

  var categories = [
    {
      code: 1,
      name: "papelaria",
      children: [
        {
          code: 12,
          name: "papel",
          parentCode: 1,
          children: []
        },
        {
          code: 13,
          name: "lapis",
          parentCode: 1,
          children: []
        }
      ]
    },
    {
      code: 2,
      name: "ferramentas",
      children: [
        {
          code: 21,
          name: "martelo",
          parentCode: 2,
          children: [
            {
              code: 22,
              name: "martelo de borracha",
              parentCode: 21,
              children: []
            }
          ]
        },
      ]
    },
    {
      code: 3,
      name: "móveis",
      children: []
    }
  ]
    
asked by anonymous 29.01.2018 / 17:42

1 answer

1

Your problem can be solved with recursion:

var categories = [
    {
      code: 1,
      name: "papelaria",
      children: [
        {
          code: 12,
          name: "papel",
          parentCode: 1,
          children: []
        },
        {
          code: 13,
          name: "lapis",
          parentCode: 1,
          children: []
        }
      ]
    },
    {
      code: 2,
      name: "ferramentas",
      children: [
        {
          code: 21,
          name: "martelo",
          parentCode: 2,
          children: [
            {
              code: 22,
              name: "martelo de borracha",
              parentCode: 21,
              children: []
            }
          ]
        },
      ]
    },
    {
      code: 3,
      name: "móveis",
      children: []
    }
  ]
  
  function filtrarCategorias(id, categorias, resultado){
    resultado = resultado ? resultado : [];
    for(var i = 0;i<categorias.length;i++){
      var categoria = categorias[i];
      resultado.push(categoria);
      if (categoria.code == id){
        return resultado;
      }
      if(categoria.children.length > 0){
        resultado = filtrarCategorias(id, categoria.children, resultado);
        if(resultado.length > 1)
          return resultado;
      }
      resultado.pop();
    }
    return [];
  }

console.log(filtrarCategorias(22, categories));

The logic is simple: we use a list as a stack, traversing it in depth and adding the elements to each iteration. If we find the correct element, we return the list. If we reach the last child and find nothing, we remove the elements from the pile until we reach the top again. The process repeats itself until we have gone through all the elements or found the requested element.

    
29.01.2018 / 20:32