Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

util/array

Unstable

Helper functions for working with arrays.

Globals

Functions

has(array, element)

Returns true if the given Array contains the element or false otherwise. A simplified version of array.indexOf(element) >= 0.

let { has } = require('sdk/util/array');
let a = ['rock', 'roll', 100];

has(a, 'rock'); // true
has(a, 'rush'); // false
has(a, 100); // true
Parameters

array : array
The array to search.

element : *
The element to search for in the array.

Returns

boolean : A boolean indicating whether or not the element was found in the array.

hasAny(array, elements)

Returns true if the given Array contains any of the elements in the elements array, or false otherwise.

let { hasAny } = require('sdk/util/array');
let a = ['rock', 'roll', 100];

hasAny(a, ['rock', 'bright', 'light']); // true
hasAny(a, ['rush', 'coil', 'jet']); // false
Parameters

array : array
The array to search for elements.

elements : array
An array of elements to search for in array.

Returns

boolean : A boolean indicating whether or not any of the elements were found in the array.

add(array, element)

If the given array does not already contain the given element, this function adds the element to the array and returns true. Otherwise, this function does not add the element and returns false.

let { add } = require('sdk/util/array');
let a = ['alice', 'bob', 'carol'];

add(a, 'dave'); // true
add(a, 'dave'); // false
add(a, 'alice'); // false

console.log(a); // ['alice', 'bob', 'carol', 'dave']
Parameters

array : array
The array to add the element to.

element : *
The element to add

Returns

boolean : A boolean indicating whether or not the element was added to the array.

remove(array, element)

If the given array contains the given element, this function removes the element from the array and returns true. Otherwise, this function does not alter the array and returns false.

let { remove } = require('sdk/util/array');
let a = ['alice', 'bob', 'carol'];

remove(a, 'dave'); // false
remove(a, 'bob'); // true
remove(a, 'bob'); // false

console.log(a); // ['alice', 'carol']
Parameters

array : array
The array to remove the element from.

element : *
The element to remove from the array if it contains it.

Returns

boolean : A boolean indicating whether or not the element was removed from the array.

unique(array)

Produces a duplicate-free version of the given array.

let { unique } = require('sdk/util/array');
let a = [1, 1, 1, 1, 3, 4, 7, 7, 10, 10, 10, 10];
let b = unique(a);

console.log(a); // [1, 1, 1, 1, 3, 4, 7, 7, 10, 10, 10, 10];
console.log(b); // [1, 3, 4, 7, 10];
Parameters

array : array
Source array.

Returns

array : The new, duplicate-free array.

flatten(array)

Flattens a nested array of any depth.

let { flatten } = require('sdk/util/array');
let a = ['cut', ['ice', ['fire']], 'elec'];
let b = flatten(a);

console.log(a); // ['cut', ['ice', ['fire']], 'elec']
console.log(b); // ['cut', 'ice', 'fire', 'elec'];
Parameters

array : array
The array to flatten.

Returns

array : The new, flattened array.

fromIterator(iterator)

Iterates over an iterator and returns the results as an array.

let { fromIterator } = require('sdk/util/array');
let i = new Set();
i.add('otoro');
i.add('unagi');
i.add('keon');

fromIterator(i) // ['otoro', 'unagi', 'keon']
Parameters

iterator : iterator
The Iterator object over which to iterate and place results into an array.

Returns

array : The iterator's results in an array.

find(iterator)

Iterates over given array and applies given predicate function until predicate(element) is true. If such element is found it's retured back otherwise third optional fallback argument is returned back. If fallback is not provided returns undefined.

let { find } = require('sdk/util/array');
let isOdd = (x) => x % 2;
find([2, 4, 5, 7, 8, 9], isOdd);   // => 5
find([2, 4, 6, 8], isOdd);         // => undefiend
find([2, 4, 6, 8], isOdd, null);   // => null

fromIterator(i) // ['otoro', 'unagi', 'keon']
Parameters

iterator : iterator
The Iterator object over which to iterate and place results into an array.

Returns

array : The iterator's results in an array.

Document Tags and Contributors

 Contributors to this page: wbamberg, jsantell
 Last updated by: wbamberg,