I have this example:
var name;
var id;
var array = [];
$.each(data, function(index, element) {
name = element.name;
id = element.id;
array[id] = name;
<a href="#" onClick="myFunction(array)">send</a>
console.log(array);
});
In this case .each
will iterate 5 times and id
will become 1, 2, 3, 4, 5
and name
will change to five names
i would like to create a multidimensional array or an object that will look like this:
[1:name1] for the first iteration
[2:name2] for the second on
...
the pass each pair of values to the myFunction
function
and inside that function to have access to the array values:
function myFunction(array){ // alert the key and value }
Any ideas how can I accomplish this scenario?
associative arrays
instead of multi-dimensional. Alternatively, since you're using numbers starting with 1, use a standard single-dimensional array and just ignore the zero-indexed item. – Goran Mottram Jan 25 '12 at 10:13