1

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++
        })
1
  • Can you post the code inside the each statement so I can figure out what you're trying to accomplish? Commented Apr 17, 2015 at 15:39

1 Answer 1

1

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);
});
Sign up to request clarification or add additional context in comments.

2 Comments

The object already has a closing '}', is outside the code section in the question, for a misprint..
@MisterFrank Updated, hope that helps.

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.