简体   繁体   中英

Access json passing variable name as string

I've some json files included in the page like this:

<script type="text/javascript" language="javascript" src="json/divaniModerni.json"></script>
<script type="text/javascript" language="javascript" src="json/divaniClassici.json"></script>

All of them having same structure, containing different elements:

var divaniModerni = {
"modelli": [
    {
        "nome": "California",
        "num": "5", 
    },
    {
        "nome": "Terra",
        "num": "6", 
    },
    {
        "nome": "Laura",
        "num": "7", 
    },
    {
        "nome": "Nonstop",
        "num": "11",    
    },
    {
        "nome": "Venere",
        "num": "8", 
    },
    {
        "nome": "Comfort",
        "num": "5", 
    },
    {
        "nome": "Infinity",
        "num": "8", 
    },
]

}

I'm now able to parse the file like this:

$(divaniModerni.modelli).each(function(index, element){ (...) }

but it's possible to dinamically change the file to parse passing the name to a function, like this?

function show(category)
{
    $(category.modelli).each(function(index, element){ (...) }
}

show(divaniModerni);

I've tried with:

$(window[category].modelli).each(function(index, element){ (...) }

but It's not working...

EDIT:

Inside the each I'm dinamically creating a row on a table, based on the selected json elements:

$(divaniModerni.modelli).each(function(index, element){
            if (i == 1)
                riga += "<tr>";
            riga += "<td><figure><a class='anteprime' rel='prettyPhoto[gallery" + i + "]' href='images/divani/" + element.nome + ".jpg'><img src='images/anteprima/divani/" + element.nome + ".jpg' alt='" + element.nome + "'></a><div class='descrizione'>" + element.nome;
            if (element.num > 0)
            {
                for (j = 2; j <= element.num; j++) 
                {
                    riga += "<a style='display:none;' class='anteprime' rel='prettyPhoto[gallery" + i + "]' href='images/divani/" + element.nome + j +  ".jpg'><img src='images/anteprima/divani/" + element.nome + j + ".jpg' alt='" + element.nome + "'></a>";
                }
            }
            riga += "</div></figure></td>"; 
            if (i == categoria.modelli.length)
            {
                riga += "</tr>";
                $('#mostra').append(riga);
            }
            else if (i % 4 == 0)
            {
                riga += "</tr>";
                $('#mostra').append(riga);  
                riga = "<tr>";  
            }   
            i++
        })

Pass in the object reference directly into the each statement to only iterate on that.

http://jsfiddle.net/b3zdahc0/

var divaniModerni = {
"modelli": [
    {
        "nome": "California",
        "num": "5", 
    },
    {
        "nome": "Terra",
        "num": "6", 
    },
    {
        "nome": "Laura",
        "num": "7", 
    },
    {
        "nome": "Nonstop",
        "num": "11",    
    },
    {
        "nome": "Venere",
        "num": "8", 
    },
    {
        "nome": "Comfort",
        "num": "5", 
    },
    {
        "nome": "Infinity",
        "num": "8", 
    }
    ]
};

function show(category)
{
    $.each(category.modelli, function(index, element) {
               alert(index)
    });

}

$("button").on("click", function(e) { 
    show(divaniModerni);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM