yes, i made a fool mistake, and mislead jquery.

thanks a lot ;)


On Fri, Aug 28, 2009 at 7:05 PM, Nick Fitzsimons<n...@nickfitz.co.uk> wrote:
>
> 2009/8/28 davidshe...@googlemail.com <davidshe...@googlemail.com>:
>> i have an array initialized like
>> var myArr = new Array();
>>
>> later I use
>>
>> myArray['somestring'] = new Array();
>> myArray['somestring'].push(mydata);
>>
>> to create and new array inside of it, and populate my data by pushing.
>> Then I want to use the jQuery.each(...) to iterator over it, like this
>>
>> $.each(myArray, function(){
>> alert(this);
>> });
>>
>> But I found the jQuery does not event enter the loop. What's wrong
>> with my array?
>>
>
> Your outermost array isn't being used as an array, it's being used as
> an object with named properties. As explained in the first paragraph
> of <http://docs.jquery.com/Utilities/jQuery.each>, $.each will see
> that it is an array, and try to iterate over its elements by numeric
> index, but as its length is zero, there aren't any.
>
> Change your outermost array to an object, and it should work:
>
> var myStuff = {};
> myStuff['somestring'] = [];
> myStuff['somestring'].push(1);
> myStuff['somestring'].push(2);
> myStuff['somestring'].push(3);
>
> $.each(myStuff, function(key, value) {
>    alert(key + ": " + value); // shows "somestring: 1,2,3"
> });
>
> Regards,
>
> Nick.
> --
> Nick Fitzsimons
> http://www.nickfitz.co.uk/
>



-- 
Best Regards,
David Shen

http://twitter.com/davidshen84

Reply via email to