I have problem understanding arrays and loops, therefore this task is a bit confusing to me. Here's my stuff;
JSON
{
"states": [
{
"name":"johor" ,
"command":"view_johor" },
{
"name":"selangor" ,
"command":"view_selangor" },
{
"name":"melaka" ,
"command":"view_melaka" },
{
"name":"kuala lumpur" ,
"command":"view_kl" },
{
"name":"penang" ,
"command":"view_penang" }
]
}
JAVASCRIPT
$(function(){
$.ajax({
type : 'GET',
url : 'scripts/list.json',
async : false,
beforeSend : function(){/*loading*/},
dataType : 'json',
success : function(result){
$.each(result, function(index, val){
for(var i=0; i < val.length; i++){
var item = val[i];
console.log(item.name)
}
});
},
});
});
My problem is I don't know how to use the loop it so that my HTML will return like this:
<ul>
<li><a href="#view_johor">Johor</a></li>
<li><a href="#view_selangor">Selangor</a></li>
<!-- and so on, dynamically depending on json... -->
</ul>
I can access the data via console.log(item.name) and such, but I can't manipulate the data so that it will display like I wanted. I don't even know the term to use to search for questions, as I know this is like basic array stuff....Thank you in advance for your help!