Iterate collection of objects within another

Asked

Viewed 24 times

1

Good, I have a json that I wanted to iterate but within it there is another that I also wanted to iterate

 var json = {
   "categories":[
      {
         "id":1,
         "name":"Best Seller",
         "childs":[
            {
               "name":"Novidades",
               "id":2,
               "parentID":1,
               "productCount":1
            },
            {
               "name":"quatro",
               "id":7,
               "parentID":1,
               "productCount":0
            }
         ]
      },
      {
         "id":2,
         "name":"Novidades",
         "childs":[
            {
               "name":"tres",
               "id":6,
               "parentID":2,
               "productCount":0
            }
         ]
      },
      {
         "id":6,
         "name":"tres",
         "childs":[

         ]
      },
      {
         "id":7,
         "name":"quatro",
         "childs":[

         ]
      },
      {
         "id":8,
         "name":"cinco",
         "childs":[
            {
               "name":"seis",
               "id":9,
               "parentID":8,
               "productCount":0
            }
         ]
      },
      {
         "id":9,
         "name":"seis",
         "childs":[

         ]
      }
   ],
   "productTypes":[
      {
         "key":0,
         "value":"Undefined"
      },
      {
         "key":1,
         "value":"Book"
      }
   ]
}

How could I push Childs into another array knowing that childs.length > 0 ?

1 answer

1


it’s no secret, just make one go inside the other.

var json = {
  "categories":[
    {
      "id":1,
      "name":"Best Seller",
      "childs":[
        {
          "name":"Novidades",
          "id":2,
          "parentID":1,
          "productCount":1
        },
        {
          "name":"quatro",
          "id":7,
          "parentID":1,
          "productCount":0
        }
      ]
    },
    {
      "id":2,
      "name":"Novidades",
      "childs":[
        {
          "name":"tres",
          "id":6,
          "parentID":2,
          "productCount":0
        }
      ]
    },
    {
      "id":6,
      "name":"tres",
      "childs":[

      ]
    },
    {
      "id":7,
      "name":"quatro",
      "childs":[

      ]
    },
    {
      "id":8,
      "name":"cinco",
      "childs":[
        {
          "name":"seis",
          "id":9,
          "parentID":8,
          "productCount":0
        }
      ]
    },
    {
      "id":9,
      "name":"seis",
      "childs":[

      ]
    }
  ],
  "productTypes":[
    {
      "key":0,
      "value":"Undefined"
    },
    {
      "key":1,
      "value":"Book"
    }
  ]
}

var children = [];
for (var prop in json.categories) {
  var category = json.categories[prop];
  // se category.childs for diferente de null, undefined, 0, "", [], {}, etc.
  if (category.childs) {
    for (var prop in category.childs) {
      var child = category.childs[prop];
      children.push(child);
    }
  }
}
console.log(children);

Browser other questions tagged

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