Search and view objects items nested with Javascript

Asked

Viewed 477 times

1

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

Example, in id 22 (rubber hammer) cado I would have to display not only the rubber hammer but also his 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: []
    }
  ]
  • Give an example of output.

  • For example if I filter by id 22, which is from the rubber hammer item, I would have to return an array (or any other form) of the code objects 2, 21 and 22.

  • Right, but will this include the kids as well? What is the exact format of the output? Show the expected output for the example you gave. You can use the [Edit] button to add the details to the question.

  • Also, you already have some code and are giving problem or are wondering about the algorithm to be used?

  • I did the code but I couldn’t bring in three levels of category, and I don’t have that code anymore. Using the rubber hammer example the output can look like this: [ {code: 2, name: "tools"}, {code: 21, name: "hammer"}, {code: 22, name: "rubber hammer"} ]

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, going through it in depth and adding the elements to each iteration. If we find the right element, we return the list. If we get to the last child and we don’t find anything, we’ll be pulling the elements out of the pile until we get to the top again. The process repeats until we have traversed all the elements or found the searched element.

  • Show, that was it!! Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.