0

I have the following object:

{
  "1":{
    "ProductType":"car",
    "ProductId":"1",
    "Brand":"Tesla",
  },
  "2":{
    "ProductType":"car",
    "ProductId":"2",
    "Brand":"Ferrari",
  }
  etc...
}

Now i need to create a loop that iterates through the object, and i need the product id and the brand and display those. How do i do that?

I have looked at dozens of stack overflow questions and i don't find any that are about exactly my use case.

Wouter G.
  • 75
  • 6

1 Answers1

0

If you want to do this using jQuery, you're looking for jQuery.each().

var data = {
  "1":{
    "ProductType":"car",
    "ProductId":"1",
    "Brand":"Tesla",
  },
  "2":{
    "ProductType":"car",
    "ProductId":"2",
    "Brand":"Ferrari",
  }
}

$.each(data, function(i, obj) {
  console.log(i+': '+obj.ProductType+' | '+obj.ProductId+' | '+obj.Brand);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
vi5ion
  • 1,028
  • 7
  • 11