The Gallery is deprecated. No new Gallery components may be submitted, and modifying existing components is disabled. For more information please read the Gallery Deprecation blog post. This is a static snapshot of the Gallery for archive purposes.
See 42 more by this user. Created: 02/11/12
Last CDN Push: 11/14/13
Build Tag: gallery-2013.11.14-01-08
Project: YUI 3
License: YUI BSD
YUI Version: 3.5.1
Free for use.
Y.Array.each and Y.Array.some are useful methods with a convenient API. It is very common to need to iterate the items of an array starting with the first item moving forward one-by-one. Occasionally, it is necessary to begin iterating part way through the array, or to iterate backwards, or to skip items in the array at regular intervals. To accomplish this it was necessary to fall back to using for loops forsaking the convenient API provided by these methods.
Y.Array.iterate aims to provide the same convenient API to replace any standard for loop. It works similarly to Y.Array.some except the start index and increment values can be specified.
This is a potential solution to Ticket #2531344: http://yuilibrary.com/projects/yui3/ticket/2531344
Y.Array.iterate receives the following arguments:
array - the array to iterate
startIndex - (Optional) The first index to iterate. If left undefined, iteration will either start at the beginning of the array if incrementBy is positive or at the end of the array if incrementBy is negative.
incrementBy - the interval by which the array will be iterated. Must be a non-zero integer. Negative values cause the array to be iterated backwards.
iterationFunction - the function to call on each iteration. This function will receive three arguments: value, index, and array. If this function returns a truthy value, iteration will be terminated.
contextObject - (Optional) the context that will become this in the iterationFunction
Y.Array.iterate will return true if iteration was terminated early, otherwise it will return false.
<script src="http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js"></script>
YUI({
//Last Gallery Build of this module
gallery: 'gallery-2013.11.14-01-08'
}).use('gallery-array-iterate', function(Y) {
var a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21],
values = [];
Y.Array.iterate(a, -4, function (value, index) {
values.push('a[' + index + '] = ' + value);
if (index === 13) {
return true;
}
});
alert(values.join(', '));
// a[21] = 21, a[17] = 17, a[13] = 13
});
All code on this site is licensed under the BSD License unless stated otherwise.