Opened 14 years ago

Closed 14 years ago

Last modified 12 years ago

#534 closed enhancement (wontfix)

Make dojo.lang.forEach work on objects too

Reported by: wolfram@… Owned by: sjmiles
Priority: high Milestone:
Component: Core Version: 0.2
Keywords: Cc:
Blocked By: Blocking:

Description

I am using

dojo.lang.forEach(someObject, dojo.debugShallow);

a lot, but objects had not been supported yet.

Here is a patch to foreach over objects too.

+++ src/lang/array.js (working copy) @@ -24,14 +24,31 @@

}

}

-dojo.lang.forEach = function(arr, unary_func, fix_length){

  • var isString = dojo.lang.isString(arr);
  • if(isString) { arr = arr.split(""); }
  • var il = arr.length;
  • for(var i=0; i< ((fix_length) ? il : arr.length); i++){
  • if(unary_func(arr[i], i, arr) == "break"){

+/ +* Loop through the given array, object or string and call the +* given function unary_func for each item. +* The function unary_func is called with the parameter: current element value, +* the index/property, the entire array, object or string. +* Example usage: +* dojo.lang.forEach([1,2,3,4,5], dojo.debug); prints all the numbers 1..5 using dojo.debug() +* dojo.lang.forEach({x:1, y:2, z:3}, dojo.debug); prints all the values of x,y,z +* dojo.lang.forEach("a string", dojo.debug); prints each character of the string +* dojo.lang.forEach("a string", dojo.debug, 3); prints the first three chars of the string +* +* @param object Either an array, object or string to loop through. +* @param function A reference to a function that shall be called for each element. +* @param int The maximum elements to loop through. +*/ +dojo.lang.forEach = function(arr /* object */, unary_func /* function */, fix_length /* int */){ + if(dojo.lang.isString(arr)) { arr = arr.split(""); } + var count = 0; + for (var i in arr){ + If the unary_func returns the string "break", quit the forEach(), see #1078. + When three arguments are given, means fix_length is given, evaluate it.

+ if(unary_func(arr[i], i, arr) == "break"
(arguments.length==3 && fix_length == count+1)){

break;

}

+ count++;

}

}

Attachments (3)

array.diff (1.8 KB) - added by wolfram@… 14 years ago.
array.diff
lang.array.diff (2.2 KB) - added by wolfram@… 14 years ago.
forEach now loops over arrays using the index, objects using for(.. in obj)
lang.array.2.diff (2.2 KB) - added by wolfram.kriesing@… 14 years ago.
Stupid copy+paste bug (fix_length didnt work on arrays), fixed

Download all attachments as: .zip

Change History (9)

Changed 14 years ago by wolfram@…

Attachment: array.diff added

array.diff

comment:1 Changed 14 years ago by wolfram@…

It might make sense to move it into dojo/lang/common.js, since it is not only for arrays

comment:2 Changed 14 years ago by sjmiles

Milestone: 0.3release
Owner: changed from anonymous to sjmiles

We cannot use for..in on arrays in general because users tend to stuff their own properties into Array.

Perhaps if you made a forEachProp function?

Is there any reason to overload forEach with a type-check versus having a separate function for object properties?

Changed 14 years ago by wolfram@…

Attachment: lang.array.diff added

forEach now loops over arrays using the index, objects using for(.. in obj)

comment:3 Changed 14 years ago by wolfram@…

Sometimes u dont know what you are looping over, it would just require you to put the check up front. i see no advantage. Putting it in the forEach would be best imho, as the last attachment provides it.

Changed 14 years ago by wolfram.kriesing@…

Attachment: lang.array.2.diff added

Stupid copy+paste bug (fix_length didnt work on arrays), fixed

comment:4 Changed 14 years ago by sjmiles

Resolution: wontfix
Status: newclosed

I'm not sure how to mark this, so I'm setting it to 'wontfix'. Yes, I submitted this patch, but it is now undone. The general problem is that it would be hard to write one function that could reliably differentiate between arrays, 'array-like' objects, and plain objects. But this is moot because we decided to make forEach conform to the Gecko spec anyway. We could make a forEachProperty function to iterate over properties, but it just doesn't seem worth it.

comment:5 Changed 14 years ago by wolfram.kriesing@…

Its a pitty, but if there is no way around ...

A couple comments I still have anyway

  • The conformance to the Gecko spec should be mentioned in the function header comment. (I don't see that it is obvious that dojo has to conform to the Gecko spec's function, where can I read about the reasons?)
  • What are the dojo.lang.is*() functions for if not for differentiating "between arrays, 'array-like' objects, and plain objects"

Thanks for your time sjmiles and sorry for the work for nothing.

comment:6 Changed 12 years ago by (none)

Milestone: 0.3release

Milestone 0.3release deleted

Note: See TracTickets for help on using tickets.