Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.
From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).
From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.
Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.
A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.
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.