Report an issue
Class

CKEDITOR.tools.array

class since 4.6.1

The namespace with helper functions and polyfills for arrays.

Filtering

Methods

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

    since 4.8.0

    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 = this.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.

    Properties
    value : Mixed

    The currently iterated array value.

    index : Number

    The index of the currently iterated value in an array.

    array : Array

    The original array passed as the array variable.

    [ 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.

    Properties
    value : Mixed

    The currently iterated array value.

    index : Number

    The index of the currently iterated value in an array.

    array : Array

    The original array passed as the array variable.

    [ thisArg ] : Mixed

    A context object for fn.

    Defaults to undefined

    Returns

    Array

    The filtered array.

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

    since 4.12.0

    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.

    Properties
    value : Mixed

    The currently iterated array value.

    index : Number

    The index of the currently iterated value in an array.

    array : Array

    The original array passed as an array variable.

    [ 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.

    Properties
    value : Mixed

    The currently iterated array value.

    index : Number

    The index of the currently iterated value in an array.

    array : Array

    The original array passed as an array variable.

    [ 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.

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

    since 4.6.2

    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.

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

    since 4.6.2

    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.