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
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
CKEDITOR.tools.array#filter
Returns a copy of
array
filtered using thefn
function. Any elements that thefn
will returnfalse
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 thatfn
returned afalse
-alike value for will be filtered out of thearray
.[ thisArg ] : Mixed
A context object for
fn
.Defaults to
undefined
Returns
Array
The filtered array.
-
Returns the first element in the array for which the given callback
fn
returnstrue
.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 returnstrue
.[ thisArg ] : Mixed
The context object for
fn
.Defaults to
undefined
Returns
*
The first matched value or
undefined
otherwise.
-
forEach( array, fn, [ thisArg ] )
CKEDITOR.tools.array#forEach
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
CKEDITOR.tools.array#indexOf
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
CKEDITOR.tools.array#isArray
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, 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 : 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.
-
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.
-
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.
-
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.
-
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.