Clear Up
SharpKit Reference

Array Class

A set of useful static methods to deal with arrays; provide missing methods for older browsers.

Namespace: Ext

Base Types

System.Object

Constructors

Methods

Name Description
clean(JsArray) Filter through an array and remove empty item as defined in Ext.isEmpty See filter
clone(JsArray) Clone a flat array without referencing the previous one. Note that this is different from Ext.clone since it doesn't handle recursive cloning. It's simply a convenient, easy-to-remember method for Array.prototype.slice.call(array)
contains(JsArray, object) Checks whether or not the given array contains the specified item
difference(JsArray, JsArray) Perform a set difference A-B by subtracting all items in array B from array A.
each(object, Delegate, object, object) Iterates an array or an iterable value and invoke the given callback function for each item.
var countries = ['Vietnam', 'Singapore', 'United States', 'Russia'];
            Ext.Array.each(countries, function(name, index, countriesItSelf) {
            console.log(name);
            });
            var sum = function() {
            var sum = 0;
            Ext.Array.each(arguments, function(value) {
            sum += value;
            });
            return sum;
            };
            sum(1, 2, 3); // returns 6
            
The iteration can be stopped by returning false in the function callback.
  Ext.Array.each(countries, function(name, index, countriesItSelf) {
            if (name === 'Singapore') {
            return false; // break here
            }
            });
            
Ext.each is alias for Ext.Array.each
erase(JsArray, JsNumber, JsNumber) Removes items from an array. This is functionally equivalent to the splice method of Array, but works around bugs in IE8's splice method and does not copy the removed elements in order to return them (because very often they are ignored).
every(JsArray, Delegate, object) Executes the specified function for each array element until the function returns a falsy value. If such an item is found, the function will return false immediately. Otherwise, it will return true.
filter(JsArray, Delegate, object) Creates a new array with all of the elements of this array for which the provided filtering function returns true.
flatten(JsArray) Recursively flattens into 1-d Array. Injects Arrays inline.
forEach(JsArray, Delegate, object) Iterates an array and invoke the given callback function for each item. Note that this will simply delegate to the native Array.prototype.forEach method if supported. It doesn't support stopping the iteration by returning false in the callback function like each. However, performance could be much better in modern browsers comparing with each
from(object, object) Converts a value to an array if it's not already an array; returns:
  • An empty array if given value is undefined or null
  • Itself if given value is already an array
  • An array copy if given value is iterable (arguments, NodeList and alike)
  • An array with one item which is the given value, otherwise
  • include(JsArray, object) Push an item into the array only if the array doesn't contain it yet
    indexOf(JsArray, object, object) Get the index of the provided item in the given array, a supplement for the missing arrayPrototype.indexOf in Internet Explorer.
    insert(JsArray, JsNumber, JsArray) Inserts items in to an array.
    intersect(JsArray, JsArray, object) Merge multiple arrays into one with unique items that exist in all of the arrays.
    map(JsArray, Delegate, object) Creates a new array with the results of calling a provided function on every element in this array.
    max(object, object) Returns the maximum value in the Array.
    mean(JsArray) Calculates the mean of all items in the array.
    merge(JsArray, JsArray, object) Merge multiple arrays into one with unique items. union is alias for merge
    min(object, object) Returns the minimum value in the Array.
    pluck(object, JsString) Plucks the value of a property from each item in the Array. Example:
      Ext.Array.pluck(Ext.query("p"), "className"); // [el1.className, el2.className, ..., elN.className]
                
    push(JsArray, object) Pushes new items onto the end of an Array. Passed parameters may be single items, or arrays of items. If an Array is found in the argument list, all its elements are pushed into the end of the target Array.
    remove(JsArray, object) Removes the specified item from the array if it exists
    replace(JsArray, JsNumber, JsNumber, object) Replaces items in an array. This is functionally equivalent to the splice method of Array, but works around bugs in IE8's splice method and is often more convenient to call because it accepts an array of items to insert rather than use a variadic argument list.
    slice(JsArray, JsNumber, JsNumber) Returns a shallow copy of a part of an array. This is equivalent to the native call "Array.prototype.slice.call(array, begin, end)". This is often used when "array" is "arguments" since the arguments object does not supply a slice method but can be the context object to Array.prototype.slice.
    some(JsArray, Delegate, object) Executes the specified function for each array element until the function returns a truthy value. If such an item is found, the function will return true immediately. Otherwise, it will return false.
    sort(JsArray, object) Sorts the elements of an Array. By default, this method sorts the elements alphabetically and ascending.
    splice(JsArray, JsNumber, JsNumber, object) Replaces items in an array. This is equivalent to the splice method of Array, but works around bugs in IE8's splice method. The signature is exactly the same as the splice method except that the array is the first argument. All arguments following removeCount are inserted in the array at index.
    sum(JsArray) Calculates the sum of all items in the given array.
    toArray(object, object, object) Converts any iterable (numeric indices and a length property) into a true array.
    function test() {
                var args = Ext.Array.toArray(arguments),
                fromSecondToLastArgs = Ext.Array.toArray(arguments, 1);
                alert(args.join(' '));
                alert(fromSecondToLastArgs.join(' '));
                }
                test('just', 'testing', 'here'); // alerts 'just testing here';
                // alerts 'testing here';
                Ext.Array.toArray(document.getElementsByTagName('div')); // will convert the NodeList into an array
                Ext.Array.toArray('splitted'); // returns ['s', 'p', 'l', 'i', 't', 't', 'e', 'd']
                Ext.Array.toArray('splitted', 0, 3); // returns ['s', 'p', 'l']
                
    Ext.toArray is alias for Ext.Array.toArray
    toMap(object, object, object) Creates a map (object) keyed by the elements of the given array. The values in the map are the index+1 of the array element. For example:
    var map = Ext.Array.toMap(['a','b','c']);
               // map = { a: 1, b: 2, c: 3 };
                
    Or a key property can be specified:
    var map = Ext.Array.toMap([
               { name: 'a' },
               { name: 'b' },
               { name: 'c' }
               ], 'name');
               // map = { a: 1, b: 2, c: 3 };
                
    Lastly, a key extractor can be provided:
    var map = Ext.Array.toMap([
               { name: 'a' },
               { name: 'b' },
               { name: 'c' }
               ], function (obj) { return obj.name.toUpperCase(); });
               // map = { A: 1, B: 2, C: 3 };
                
    union(JsArray, JsArray, object) Merge multiple arrays into one with unique items. union is alias for merge
    unique(JsArray) Returns a new array with unique items
    © Copyright 2005-2011 SharpKit. All rights reserved.