I'm trying to get my head around arrays in jQuery. Let's take the following scenario. I have an array of data, like so:
var myVariable = [
'key1' : 'value1',
'key2' : 'value2',
'key3' : 'value3',
'key4' : 'value4'
]
And here is a list of elements:
<ul id="elem">
<li class="key1">Item 1</li>
<li class="key2">Item 2</li>
<li class="key3">Item 3</li>
<li class="key4">Item 4</li>
</ul>
How would I go about creating an if statement to run through each element, if the class is identified in the array then replace the text content? So the final output should be:
<ul id="elem">
<li class="key1">key1</li>
<li class="key2">key2</li>
<li class="key3">key3</li>
<li class="key4">key4</li>
</ul>
I have tried with the following but it doesn't appear to work.
$('#elm li').each(function (index) {
$(this).text(myVariable[index]);
});
I realise there aren't associative arrays in JS but I'm not really understanding how it work.
Please point me in the right direct. Thanks!!
[ ]which are for creating arrays.