Jump to content

[jQuery] "Joining" two arrays with jQuery loop?


fishbaitfood

Recommended Posts

Hello there,

 

I have two arrays with an equal amount of values.

array fileList: names of files

array file_numList: numbers of files

 

I want those values to match their respective other, with a loop.

Output would look something like this:

 

for (var i = 0; i < fileList.length ; i++) {
$("div.box").append("<input type=\"radio\" value=\""+file_numList[i]+"\" />"+fileList[i]);
}

 

This won't work however.

And with an .each() loop, I can't seem to get an i counter?

 

How would I do this?

If the keys match, within the callback function you can define two parameters. The first being the key and the second being the value. As an example, run this:

 

<script>
    $(document).ready(function()
    {
        $(['one', 'two', 'three']).each(function(key, value)
        {
            alert('Key: ' + key + ' / Value: ' + value);
        });
    });
</script>

 

Using the key you can just look-up the value in the other array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.