How To Create List From JSON Array?

前端 未结 5 846
渐次进展
渐次进展 2021-01-05 20:44

I have problem understanding arrays and loops, therefore this task is a bit confusing to me. Here\'s my stuff;

JSON

{
\"states\": [
         


        
5条回答
  •  渐次进展
    2021-01-05 21:17

    Making very minor adjustments to your code,

    var a = [];
    $.each(result, function(index, val){
        for(var i=0; i < val.length; i++){
            var item = val[i];
            a.push(item);
        }
    });
    

    Now do something with all of the values in the array a.

    EDIT:

    In case that wasn't enough,

    var list = $('');
    $.each(result.states, function(state) {
        var link = $('').prop('href', state.command).text(state.name);
        ('
  • ').append(link).appendTo(list); });

    list is the HTML you want (except for the name capitalization...I wasn't sure if the first letter should be capitalized, or the first letter of each word, so I left that alone).

提交回复
热议问题