Report an issue
Class

CKEDITOR.tools.array

class since 4.6.1

The namespace with helper functions and polyfills for arrays.

Filtering

Methods

  • since 4.8.0

    every( array, fn, [ thisArg ] ) → Boolean

    Tests whether all elements in an array pass the test implemented by the provided function. Returns true if the provided array is empty.

    var every = CKEDITOR.tools.array.every( [ 11, 22, 33, 44 ], function( value ) {
        return value > 10;
    } );
    console.log( every );
    // Logs: true
    

    Parameters

    array : Array
    fn : Function

    A function that gets called with each array item.

    [ thisArg ] : Mixed

    A context object for fn.

    Defaults to undefined

    Returns

    Boolean

    Information whether all elements pass the test.

  • filter( array, fn, [ thisArg ] ) → Array

    Returns a copy of array filtered using the fn function. Any elements that the fn will return false for will get removed from the returned array.

    var filtered = this.array.filter( [ 0, 1, 2, 3 ], function( value ) {
        // Leave only values equal or greater than 2.
        return value >= 2;
    } );
    console.log( filtered );
    // Logs: [ 2, 3 ]
    

    Parameters

    array : Array
    fn : Function

    A function that gets called with each array item. Any item that fn returned a false-alike value for will be filtered out of the array.

    [ thisArg ] : Mixed

    A context object for fn.

    Defaults to undefined

    Returns

    Array

    The filtered array.

  • since 4.12.0

    find( array, fn, [ thisArg ] ) → *

    Returns the first element in the array for which the given callback fn returns true.

    var array = [ 1, 2, 3, 4 ];
    
    CKEDITOR.tools.array.find( array, function( item ) {
        return item > 2;
    } ); // returns 3.
    

    Parameters

    array : Array

    An array to be iterated over.

    fn : Function

    A function called for every array element until it returns true.

    [ thisArg ] : Mixed

    The context object for fn.

    Defaults to undefined

    Returns

    *

    The first matched value or undefined otherwise.

  • forEach( array, fn, [ thisArg ] )

    Iterates over every element in the array.

    Parameters

    array : Array

    An array to be iterated over.

    fn : Function

    The function called for every array element.

    [ thisArg ] : Mixed

    The context object for fn.

    Defaults to undefined

  • indexOf( array, value ) → Number

    Returns the index of an element in an array.

    var letters = [ 'a', 'b', 0, 'c', false ];
    alert( CKEDITOR.tools.indexOf( letters, '0' ) );        // -1 because 0 !== '0'
    alert( CKEDITOR.tools.indexOf( letters, false ) );      // 4 because 0 !== false
    

    Parameters

    array : Array

    The array to be searched.

    value : Object | Function

    The element to be found. This can be an evaluation function which receives a single parameter call for each entry in the array, returning true if the entry matches.

    Returns

    Number

    The (zero-based) index of the first entry that matches the entry, or -1 if not found.

  • isArray( object ) → Boolean

    Checks if an object is an Array.

    alert( CKEDITOR.tools.isArray( [] ) );      // true
    alert( CKEDITOR.tools.isArray( 'Test' ) );  // false
    

    Parameters

    object : Object

    The object to be checked.

    Returns

    Boolean

    true if the object is an Array, otherwise false.

  • since 4.6.2

    map( array, fn, [ thisArg ] ) → Array

    Applies a function to each element of an array and returns the array of results in the same order. Note the order of the parameters.

    Parameters

    array : Array

    An array of elements that fn is applied on.

    fn : Function

    A function with the signature a -> b.

    [ thisArg ] : Mixed

    The context object for fn.

    Defaults to undefined

    Returns

    Array

    An array of mapped elements.

  • since 4.6.2

    reduce( array, fn, initial, [ thisArg ] ) → Mixed

    Applies a function against each value in an array storing the result in an accumulator passed to the next iteration. Note the order of the parameters.

    Parameters

    array : Array

    An array of elements that fn is applied on.

    fn : Function

    A function with the signature (accumulator, a, index, array) -> b.

    initial : Mixed

    Initial value of the accumulator.

    [ thisArg ] : Mixed

    The context object for fn.

    Defaults to undefined

    Returns

    Mixed

    The final value of the accumulator.

  • since 4.13.0

    some( array, fn, [ thisArg ] ) → Boolean

    Tests whether any element in an array passes the test implemented by the provided function. Returns false if the provided array is empty.

    var some = CKEDITOR.tools.array.some( [ 11, 2, 3, 4 ], function( value ) {
        return value > 10;
    } );
    console.log( some );
    // Logs: true
    

    Parameters

    array : Array
    fn : Function

    A function that gets called with each array item.

    [ thisArg ] : Mixed

    A context object for fn.

    Defaults to undefined

    Returns

    Boolean

    Information whether any element passes the test.

  • since 4.16.0

    unique( array ) → Array

    Removes duplicates from the array.

    var array = CKEDITOR.tools.array.unique( [ 1, 1, 2, 3, 2 ] );
    console.log( array );
    // Logs: [ 1, 2, 3 ]
    

    Parameters

    array : Array

    Array from which duplicates should be removed.

    Returns

    Array

    The copy of the input array without duplicates.

  • since 4.15.1

    zip( array1, array2 ) → Array

    Zips corresponding objects from two arrays into a single array of object pairs.

    var zip = CKEDITOR.tools.array.zip( [ 'foo', 'bar', 'baz' ], [ 1, 2, 3 ] );
    console.log( zip );
    // Logs: [ [ 'foo', 1 ], [ 'bar', 2 ], [ 'baz', 3 ] ];
    

    Parameters

    array1 : Array
    array2 : Array

    Returns

    Array

    A two-dimensional array of object pairs.