tracker issue : CF-3316802

select a category, or use search below
(searches all categories and all time range)
Title:

arrayEach() should pass the array index into the callback

| View in Tracker

Status/Resolution/Reason: Closed/Fixed/

Reporter/Name(from Bugbase): Adam Cameron / Adam Cameron (Adam Cameron)

Created: 08/23/2012

Components: Language, Closures

Versions: 10.0

Failure Type: Enhancement Request

Found In Build/Fixed In Build: Final / 285716

Priority/Frequency: Trivial / Unknown

Locale/System: English / Win All

Vote Count: 3

If I want to know which index each element is at, I need to infer that myself (ie: with the i variable that I maintain "by hand") .  arrayEach() would be a lot more fully-realised if its callback was also passed the index.  I think that a very high percentage of the time, one would want to know which element one is looking at, as well as the element value.  It's a bit "jerry-built" to have to roll one's own.

<cfscript>
a = ["Tahi","Rua","Toru","Wha","Rima","Ono","Whitu","Waru","Iwa","Tekau"];

arrayEach(
	a,
	function(element){
		param name="i" default=1;
		writeOutput("#i++# #uCase(element)#<br />");
	}
);
</cfscript>

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	3316802

External Customer Info:
External Company:  
External Customer Name: Adam Cameron.
External Customer Email:  
External Test Config: My Hardware and Environment details:

Attachments:

Comments:

Please implement passing the index, this is very frustrating because I want to use ArrayEach but then I have to use for loops instead in cases where I need the index.
Vote by External U.
18378 | January 30, 2013 12:01:16 PM GMT
yes, index should be passed!
Vote by External U.
18379 | June 13, 2013 12:59:48 PM GMT
Please, please, please add this! It would make it possible to use ArrayEach like an ArrayMap function, which I dearly miss.
Vote by External U.
18380 | July 02, 2013 08:05:09 AM GMT
See notes on this article: http://cfmlblog.adamcameron.me/2013/09/arrayeach-could-stand-some-improvement.html It'd be good to make two further mods to this: 1) pass the array in to the callback too as a third arg (as per Javascript) 2) only call the callback if there's actually an element at the relevant index point of the array (also how JS does it). -- Adam
Comment by External U.
18373 | September 29, 2013 04:16:17 AM GMT
Was the fix here just a matter of adding a second argument to the callback, so the form is: arrayEach(a, function(value, index){ // etc }); ? Cheers -- Adam
Comment by External U.
18374 | January 30, 2014 09:17:26 AM GMT
Apart from the element, we would pass both the index and array reference in the callback.
Comment by Rupesh K.
18375 | January 31, 2014 10:50:25 PM GMT
Cheers for the clarification, Rupesh. So it's like forEach() in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach. One last point... it's an array REFERENCE, not just a copy of the whole array? -- Adam
Comment by External U.
18376 | February 01, 2014 02:17:25 AM GMT
Yes, it is the reference
Comment by Rupesh K.
18377 | February 05, 2014 05:22:43 AM GMT