Opened 11 years ago

Closed 11 years ago

#8020 closed enhancement (invalid)

dojo.forEach() not working with associative arrays

Reported by: holgerthurow Owned by: alex
Priority: high Milestone:
Component: General Version: 1.0
Keywords: dojo.forEach Cc:
Blocked By: Blocking:

Description

Unlike JavaScript?'s for-each-Loop dojo.forEach() can't deal with associative arrays.

var test = []
test["one"] = 123
test["two"] = 456

dojo.forEach(test, function(elem){
    console.log(elem) // nothing
})

for each(var elem in test){
    console.log(elem) // 123, 456
}

Change History (2)

comment:1 Changed 11 years ago by dante

Owner: changed from anonymous to alex

dojo.forEach is meant to mimic Array.forEach in JS 1.6, which excludes "associative arrays", aka: Objects. There is a forIn function in dojox.lang.functional that behaves as you'd expect on objects, but that functionality is not currently in base dojo.forEach.

@alex - was the intention to stick strictly to the 1.6 API or including some (for in) love in forEach a possibility?

comment:2 Changed 11 years ago by Adam Peller

Resolution: invalid
Status: newclosed

The intent was to stick strictly to the 1.6 extensions, now supported by 3/4 major browsers, and we actually pass through to the native Array functions when they are available. Array.forEach operates on Arrays and Strings only. Alex can correct me if I'm wrong.

the "for each" syntax mentioned is a 1.7 Netscape/Mozilla? extension distinct from the Array functionality in their Javascript 1.6. I don't know if it has the same level of adoption, and that actually makes two ways to iterate through an object: for..in for names, and for each ... in for values. It sounds like dojox.lang.functional is the way to get what you want.

Note: See TracTickets for help on using tickets.