CKEDITOR.tools.array
The namespace with helper functions and polyfills for arrays.
Filtering
Methods
-
Tests whether all elements in an array pass the test implemented by the provided function. Returns
trueif the provided array is empty.var every = CKEDITOR.tools.array.every( [ 11, 22, 33, 44 ], function( value ) { return value > 10; } ); console.log( every ); // Logs: trueParameters
array : Arrayfn : FunctionA function that gets called with each
arrayitem.[ thisArg ] : MixedA context object for
fn.Defaults to
undefinedReturns
BooleanInformation whether all elements pass the test.
filter( array, fn, [ thisArg ] ) → ArrayCKEDITOR.tools.array#filterReturns a copy of
arrayfiltered using thefnfunction. Any elements that thefnwill returnfalsefor 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 : Arrayfn : FunctionA function that gets called with each
arrayitem. Any item thatfnreturned afalse-alike value for will be filtered out of thearray.[ thisArg ] : MixedA context object for
fn.Defaults to
undefinedReturns
ArrayThe filtered array.
Returns the first element in the array for which the given callback
fnreturnstrue.var array = [ 1, 2, 3, 4 ]; CKEDITOR.tools.array.find( array, function( item ) { return item > 2; } ); // returns 3.Parameters
array : ArrayAn array to be iterated over.
fn : FunctionA function called for every
arrayelement until it returnstrue.[ thisArg ] : MixedThe context object for
fn.Defaults to
undefinedReturns
*The first matched value or
undefinedotherwise.forEach( array, fn, [ thisArg ] )CKEDITOR.tools.array#forEachIterates over every element in the
array.Parameters
array : ArrayAn array to be iterated over.
fn : FunctionThe function called for every
arrayelement.[ thisArg ] : MixedThe context object for
fn.Defaults to
undefinedindexOf( array, value ) → NumberCKEDITOR.tools.array#indexOfReturns 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 !== falseParameters
array : ArrayThe array to be searched.
value : Object | FunctionThe element to be found. This can be an evaluation function which receives a single parameter call for each entry in the array, returning
trueif the entry matches.Returns
NumberThe (zero-based) index of the first entry that matches the entry, or
-1if not found.isArray( object ) → BooleanCKEDITOR.tools.array#isArrayChecks if an object is an Array.
alert( CKEDITOR.tools.isArray( [] ) ); // true alert( CKEDITOR.tools.isArray( 'Test' ) ); // falseParameters
object : ObjectThe object to be checked.
Returns
Booleantrueif the object is an Array, otherwisefalse.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 : ArrayAn array of elements that
fnis applied on.fn : FunctionA function with the signature
a -> b.[ thisArg ] : MixedThe context object for
fn.Defaults to
undefinedReturns
ArrayAn array of mapped elements.
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 : ArrayAn array of elements that
fnis applied on.fn : FunctionA function with the signature
(accumulator, a, index, array) -> b.initial : MixedInitial value of the accumulator.
[ thisArg ] : MixedThe context object for
fn.Defaults to
undefinedReturns
MixedThe final value of the accumulator.
Tests whether any element in an array passes the test implemented by the provided function. Returns
falseif the provided array is empty.var some = CKEDITOR.tools.array.some( [ 11, 2, 3, 4 ], function( value ) { return value > 10; } ); console.log( some ); // Logs: trueParameters
array : Arrayfn : FunctionA function that gets called with each
arrayitem.[ thisArg ] : MixedA context object for
fn.Defaults to
undefinedReturns
BooleanInformation whether any element passes the test.
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 : ArrayArray from which duplicates should be removed.
Returns
ArrayThe copy of the input array without duplicates.
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 : Arrayarray2 : Array
Returns
ArrayA two-dimensional array of object pairs.