Instance Methods
This method returns the index that a given item would be inserted into the
given (sorted) array
. Note that the given item
may or may not be in the
array. This method will return the index of where the item should be.
For example:
var array = [ 'A', 'D', 'G', 'K', 'O', 'R', 'X' ];
var index = Ext.Array.binarySearch(array, 'E');
console.log('index: ' + index);
// logs "index: 2"
array.splice(index, 0, 'E');
console.log('array : ' + array.join(''));
// logs "array: ADEGKORX"
item :
Object
The item that you want to insert into the array
.
begin :
Number
(optional)
The first index in the array
to consider.
Defaults to: 0
end :
Number
(optional)
The index that marks the end of the range
to consider. The item at this index is not considered.
Defaults to: array.length
compareFn :
Function
(optional)
The comparison function that matches the sort
order of the array
. The default compareFn
compares items using less-than
and greater-than operators.
:
Number
The index for the given item in the given array based on
the current sorters.
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)
.
:
Array
Checks whether or not the given array
contains the specified item
.
:
Boolean
true
if the array contains the item, false
otherwise.
Perform a set difference A-B by subtracting all items in array B from array A.
:
Array
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
from the callback function.
Returning undefined
(i.e return;
) will only exit the callback function and
proceed with the next iteration of the loop.
Ext.Array.each(countries, function(name, index, countriesItSelf) {
if (name === 'Singapore') {
return false; // break here
}
});
Ext.each is alias for Ext.Array.each
array :
Array/NodeList/Object
The value to be iterated. If this
argument is not iterable, the callback function is called once.
fn :
Function
The callback function. If it returns false
, the iteration
stops and this method returns the current index
. Returning undefined
(i.e
return;
) will only exit the callback function and proceed with the next iteration
in the loop.
item :
Object
The item at the current index
in the passed array
index :
Number
The current index
within the array
allItems :
Array
The array
itself which was passed as the first argument
return :
Boolean
Return false
to stop iteration.
scope :
Object
(optional)
The scope (this
reference) in which the specified function is executed.
reverse :
Boolean
(optional)
Reverse the iteration order (loop from the end to the beginning).
Defaults to: false
:
Boolean/Number
If all array entries were iterated, this will be true. If
iteration was halted early because the passed fuction returned
false`, this will
be the index at which iteration was halted.
Shallow compares the contents of 2 arrays using strict equality.
:
Boolean
true
if the arrays are equal.
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).
array :
Array
The Array on which to replace.
index :
Number
The index in the array at which to operate.
removeCount :
Number
The number of items to remove at index.
:
Array
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
.
fn :
Function
Callback function for each item.
array :
Array
The whole array that's being iterated.
:
Boolean
true
if no false value is returned by the callback function.
Creates a new array with all of the elements of this array for which
the provided filtering function returns a truthy value.
fn :
Function
Callback function for each item.
array :
Array
The whole array that's being iterated.
:
Array
Returns the first item in the array which elicits a truthy return value from the
passed selection function.
fn :
Function
The selection function to execute for each item.
index :
Number
The index of the array item.
scope :
Object
(optional)
The scope (this
reference) in which the
function is executed. Defaults to the array
:
Object
The first item in the array which returned true from the selection
function, or null if none was found.
Recursively flattens into 1-d Array. Injects Arrays inline.
:
Array
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 Ext.Array#each. However, performance
could be much better in modern browsers comparing with Ext.Array#each
fn :
Function
item :
Object
The item at the current index
in the passed array
.
index :
Number
The current index
within the array
.
allItems :
Array
The array
itself which was passed as the first argument.
scope :
Object
The execution scope (this
) in which the
specified function is executed.
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
value :
Object
The value to convert to an array if it's not already is an array.
newReference :
Boolean
(optional)
true
to clone the given array and return a new reference if necessary.
:
Array
Push an item into the array only if the array doesn't contain it yet.
Get the index of the provided item
in the given array
, a supplement for the
missing arrayPrototype.indexOf in Internet Explorer.
from :
Number
The index at which to begin the search.
:
Number
The index of item in the array (or -1 if it is not found).
Inserts items in to an array.
array :
Array
The Array in which to insert.
index :
Number
The index in the array at which to operate.
items :
Array
The array of items to insert at index.
:
Array
Merge multiple arrays into one with unique items that exist in all of the arrays.
:
Array
Creates a new array with the results of calling a provided function on every element in this array.
fn :
Function
Callback function for each item.
array :
Array
The whole array that's being iterated.
scope :
Object
(optional)
:
Array
Returns the maximum value in the Array.
array :
Array/NodeList
The Array from which to select the maximum value.
comparisonFn :
Function
(optional)
a function to perform the comparison which determines maximization.
If omitted the ">" operator will be used.
Note: gt = 1; eq = 0; lt = -1
item :
Mixed
The value to compare with the current maximum.
:
Object
maxValue The maximum value.
Calculates the mean of all items in the array.
array :
Array
The Array to calculate the mean value of.
:
Number
Returns the minimum value in the Array.
array :
Array/NodeList
The Array from which to select the minimum value.
comparisonFn :
Function
(optional)
a function to perform the comparison which determines minimization.
If omitted the "<" operator will be used.
Note: gt = 1; eq = 0; lt = -1
item :
Mixed
The value to compare with the current minimum.
:
Object
minValue The minimum value.
A function used to sort an array by numeric value. By default, javascript array values
are coerced to strings when sorting, which can be problematic when using numeric values. To
ensure that the values are sorted numerically, this method can be passed to the sort method:
Ext.Array.sort(myArray, Ext.Array.numericSortFn);
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]
array :
Array/NodeList
The Array of items to pluck the value from.
propertyName :
String
The property name to pluck from each element.
:
Array
The value from each item in the Array.
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.
target :
Array
The Array onto which to push new items
elements :
Object...
The elements to add to the array. Each parameter may
be an Array, in which case all the elements of that Array will be pushed into the end of the
destination Array.
:
Array
An array containing all the new items push onto the end.
This method applies the reduceFn
function against an accumulator and each
value of the array
(from left-to-right) to reduce it to a single value.
If no initialValue
is specified, the first element of the array is used as
the initial value. For example:
function reducer (previous, value, index) {
console.log('[' + index + ']: (' + previous + ',' + value + '}');
return previous * 10 + value;
}
v = Ext.Array.reduce([2, 3, 4], reducer);
console.log('v = ' + v);
> [1]: (2, 3)
> [2]: (23, 4)
> v = 234
v = Ext.Array.reduce([2, 3, 4], reducer, 1);
console.log('v = ' + v);
> [0]: (1, 2)
> [1]: (12, 3)
> [2]: (123, 4)
> v = 1234
Available since: 6.0.0
reduceFn :
Function
The reducing callback function.
index :
Number
The index in the array of the current value
.
array :
Array
The array to being processed.
initialValue :
Mixed
(optional)
:
Mixed
Removes the specified item from the array if it exists.
:
Array
Removes item/s at the specified index.
index :
Number
The index of the item to be removed.
count :
Number
(optional)
The number of items to be removed.
Defaults to: 1
:
Array
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.
array :
Array
The Array on which to replace.
index :
Number
The index in the array at which to operate.
removeCount :
Number
The number of items to remove at index (can be 0).
insert :
Array
(optional)
An array of items to insert at index.
:
Array
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
.
array :
Array
The array (or arguments object).
begin :
Number
The index at which to begin. Negative values are offsets from
the end of the array.
end :
Number
The index at which to end. The copied items do not include
end. Negative values are offsets from the end of the array. If end is omitted,
all items up to the end of the array are copied.
:
Array
The copied piece of the array.
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
.
fn :
Function
Callback function for each item.
array :
Array
The whole array that's being iterated.
:
Boolean
true
if the callback function returns a truthy value.
Sorts the elements of an Array in a stable manner (equivalently keyed values do not move relative to each other).
By default, this method sorts the elements alphabetically and ascending.
Note: This method modifies the passed array, in the same manner as the
native javascript Array.sort.
sortFn :
Function
(optional)
a :
Mixed
The first item to compare.
b :
Mixed
The second item to compare.
return :
Number
-1
if a < b, 1
if a > b, otherwise 0
.
:
Array
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.
array :
Array
The Array on which to replace.
index :
Number
The index in the array at which to operate.
removeCount :
Number
The number of items to remove at index (can be 0).
elements :
Object...
The elements to add to the array. If you don't specify
any elements, splice simply removes elements from the array.
:
Array
An array containing the removed items.
Calculates the sum of all items in the given array.
array :
Array
The Array to calculate the sum value of.
:
Number
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
iterable :
Object
the iterable object to be turned into a true Array.
start :
Number
(optional)
a zero-based index that specifies the start of extraction.
Defaults to: 0
end :
Number
(optional)
a 1-based index that specifies the end of extraction.
Defaults to: -1
:
Array
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 };
strings :
String/String[]
The strings from which to create the map.
getKey :
String/Function
(optional)
Name of the object property to use
as a key or a function to extract the key.
scope :
Object
(optional)
Value of this
inside callback specified for getKey
.
:
Object
Creates a map (object) keyed by a property of elements of the given array. The values in
the map are the array element. For example:
var map = Ext.Array.toValueMap(['a','b','c']);
// map = { a: 'a', b: 'b', c: 'c' };
Or a key property can be specified:
var map = Ext.Array.toValueMap([
{ name: 'a' },
{ name: 'b' },
{ name: 'c' }
], 'name');
// map = { a: {name: 'a'}, b: {name: 'b'}, c: {name: 'c'} };
Lastly, a key extractor can be provided:
var map = Ext.Array.toValueMap([
{ name: 'a' },
{ name: 'b' },
{ name: 'c' }
], function (obj) { return obj.name.toUpperCase(); });
// map = { A: {name: 'a'}, B: {name: 'b'}, C: {name: 'c'} };
array :
Array
The Array to create the map from.
getKey :
String/Function
(optional)
Name of the object property to use
as a key or a function to extract the key.
scope :
Object
(optional)
Value of this inside callback. This parameter is only
passed when getKey
is a function. If getKey
is not a function, the 3rd
argument is arrayify
.
arrayify :
Number
(optional)
Pass 1
to create arrays for all map entries
or 2
to create arrays for map entries that have 2 or more items with the
same key. This only applies when getKey
is specified. By default the map will
hold the last entry with a given key.
:
Object
Returns a new array with unique items.
:
Array