admin管理员组

文章数量:1314488

whats wrong with this jquery code. it is not outputting anyting?

var imagesToLoad = [];

var name = 'hi';
var src = 'ho';

imagesToLoad[name] = src;

$.each(imagesToLoad, function(index, value) {
 alert(index + ': ' + value);
});

basically i want to add custom variables to my object after it has been created.

whats wrong with this jquery code. it is not outputting anyting?

var imagesToLoad = [];

var name = 'hi';
var src = 'ho';

imagesToLoad[name] = src;

$.each(imagesToLoad, function(index, value) {
 alert(index + ': ' + value);
});

basically i want to add custom variables to my object after it has been created.

Share Improve this question asked Feb 4, 2011 at 11:51 clampclamp 34k75 gold badges207 silver badges305 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Javascript arrays don't support non numerical indexes. You probably want to use an object instead:

var imagesToLoad = {};
imagesToLoad.hi = 'ho';

$.each(imagesToLoad, function(index, value) {
 alert(index + ': ' + value);
});

You should check the doc for $.each method - it accepts only callback function as parameter and it can iterate only over jQuery object

本文标签: javascriptjquery adding custom key value to objectStack Overflow