0

I have this simple JSON

{"Name":"Srbija","Brands":[{"Name":"Citroen","Models":[]},{"Name":"Dacia","Models":[]},{"Name":"Ford","Models":[]},{"Name":"Hyundai","Models":[]},{"Name":"Kia","Models":[]},{"Name":"Mazda","Models":[]},{"Name":"Nissan","Models":[]},{"Name":"Opel","Models":[]},{"Name":"Peugeot","Models":[]},{"Name":"Renualt","Models":[]},{"Name":"Skoda","Models":[]},{"Name":"Suzuki","Models":[]},{"Name":"Toyota","Models":[]},{"Name":"Volkswagen","Models":[]}]}

What i need is to display Brands name in UL list

This is what i have for now, but i got undefinded

JS

 function countryBrands(data, textStatus, jqXHR){
            console.log(data);
            var output="<ul>";
            for (var i in data) 
            {
                output+="<li>" + data[i].Brands + ",  " + data[i].Name + "</li>";
            }
            output+="</ul>";
            $('#test').html(output);
        }

HTML

<div id="test"></div>

3 Answers 3

1

Since Brands is an array loop through Brands and not data.

        console.log(data);
        var output="<ul>";
        for (var i in data.Brands) 
        {
            output+="<li>" + data.Brands[i].Name + ",  " + data.Name + "</li>";
        }
        output+="</ul>";
        $('#test').html(output);
Sign up to request clarification or add additional context in comments.

2 Comments

Can you help me foreach this json carportal.azurewebsites.net/api/portal/…, i only need model name
@MiomirDancevic here you go.. jsfiddle.net/bipen/3r6rwuw2/1 ... you have to loop in twice to get that. if for is confusing then use each().
0
  1. Use .each() to iterate the data

var data = {
  "Name": "Srbija",
  "Brands": [{
    "Name": "Citroen",
    "Models": []
  }, {
    "Name": "Dacia",
    "Models": []
  }, {
    "Name": "Ford",
    "Models": []
  }, {
    "Name": "Hyundai",
    "Models": []
  }, {
    "Name": "Kia",
    "Models": []
  }, {
    "Name": "Mazda",
    "Models": []
  }, {
    "Name": "Nissan",
    "Models": []
  }, {
    "Name": "Opel",
    "Models": []
  }, {
    "Name": "Peugeot",
    "Models": []
  }, {
    "Name": "Renualt",
    "Models": []
  }, {
    "Name": "Skoda",
    "Models": []
  }, {
    "Name": "Suzuki",
    "Models": []
  }, {
    "Name": "Toyota",
    "Models": []
  }, {
    "Name": "Volkswagen",
    "Models": []
  }]
};


var output = "";
$.each(data.Brands, function(i,v) {

  output += "<li>"+ v.Name + "</li>";

})


output += "</ul>";
$('#test').html(output);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test"></div>

Comments

0

You can use Array map function too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.