• Register
  • Log In
  • Home
  • Quick Start
    • Configurator
    • Download YUI 3
  • Documentation
    • User Guides
    • Examples
    • API Docs
    • Environments
    • Tutorials
  • Community
    • Gallery
    • Blog
    • Forums
    • YUI Theater
    • Calendar
  • Contribute
    • YUI on GitHub »
    • File a Ticket
    • View Tickets
    • Dashboard
  • Other Projects
    • Shifter »
    • Yogi »
    • YUI 2
    • YUI Doc »
    • YUI Test
    • YUI Website
    • YUI Compressor »
    • YUI Builder »
    • YUI PHP Loader
    • Grid Builder »
    • Skin Builder »
  • YUI
  • >
  • Community
  • >
  • Gallery

Gallery

Modules

  • Home
  • Featured
  • Popular
  • New
  • All

Documentation

  • Yogi Documentation
  • Shifter Documentation
  • Developer Guide
  • Module Setup

Tag Cloud

Context Navigation

    YUI Library is not responsible for bugs or support with this module. It is available as a free service. For support please contact the module owner with the provided links.

    Array Iterate (gallery-array-iterate) on cdn

    Last Updated: 06/21/12
    + 0 -

    Steven Olmsted

    YUI Contributor

    See 42 more by this user.

    Created: 02/11/12
    Last CDN Push: 05/2/13
    Build Tag: gallery-2013.05.02-22-59
    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

    • Tags:
    • some
    • solmsted
    • loop
    • iterate
    • for
    • each
    • array
    • Download
    • Docs
    • Homepage
    • Bugs
    • Source
    • Example
    • Forum
    • History

    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.

    Code Sample

    <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.05.02-22-59'
    }).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
    });

    © 2006-2013 Yahoo! Inc. All rights reserved.
    All code on this site is licensed under the BSD License unless stated otherwise.
    About This Site · Security Contact Info