Arrays in JavaScript
The Array
object is a class that enables you to store a set of elements in
order under a single variable name. This class has methods to perform the most
common Array operations.
Array
in JS have a variable length and can store elements of any type. You can access an arbitrary position of the array, without being consecutive with the last item. This fact implies you can have a non-dense array.
let myList = [0, 1, 2]; myList[6] = 6; console.log(myList.length); console.log(myList); for (let pos of myList) { console.log(pos); }
Also, we can make the array smaller by changing the array length
let myList = []; let n = 100; console.log(myList.length); for (let i = 0; i < n; ++i) myList.push(i); // append the new value console.log(myList.length); myList.length = 3; console.log(myList); console.log(myList.length);
Iterate over an array
You can iterate over all the elements from the array in different ways, by using
the JavaScript for
syntax
let myList = [1, 2, 3]; for (let i = 0; i < myList.length; ++i) console.log(myList[i]) // access an item by its position for (let value of myList) console.log(value) // value is assigned in each iteration to the next item in the array for (let pos in myList) console.log(pos) // Iter over the keys of the array, in this case, the position
Some methods from the Array class
concat
: Concatenates 2 arrays and returns a new onelet a = [1, 2, 3], b = [4, 5, 6]; let c = a.concat(b); console.log(c); console.log(a);
every
: This method gets a function as an argument and returnstrue
if all the items in the array return a non-false value.let a = [2, 6, "hola", []]; function test(item) { console.log(item); return item; } console.log(a.every(test)); a.push(""); console.log(a.every(test));
filter
: Returns a newarray
with the items that returntrue
by the provided function.let old = []; for (let i = 0; i < 50; ++i) old.push(i); let noPrime = new Set(); let prime = old.filter(function(number) { if (number in noPrime) return false; for (let i = 2, s = Math.sqrt(number); i <= s; ++i) { if (number % i === 0) { noPrime.add(i); noPrime.add(number/i); return false; } } return true }); console.log(prime);
fill
: Changes items of the array with a static value into the range passed as argumentslet a = []; a.length = 10; console.log(a); let value = 0, startPos = 4, endPos = 6; console.log(a.fill(value, startPos, endPos));
find
,findLast
,findIndex
: These methods get a callback function and return the first and last item, respectively, from the array which the callback function return true. In the case offindIndex
, it returns the position of the first matching item.let a = [1, 2, 3, 5, 7, 10, 11]; console.log(a.find(function(item) { return item%5 === 0 })); console.log(a.findLast(function(item) {return item%5 === 0})); console.log(a.findIndex(function(item) { return item%5 === 0 }));
forEach
: Executes a function for each element in the array, without editing itincludes
: check if an item is in the arrayindexOf
,lastIndexOf
: return the position of the first or last item found in the array; -1 if the argument does not exist.join
: This method joins all items from the array using the character passed as argumentmap
: Run a function for each item of thearray
and return a new one with the same length and the values returned by the functionlet names = "john smith"; let tmpNames = names.split(" ").map(function(name) { return name[0].toUpperCase() + name.slice(1); }); console.log(tmpNames.join(" "));
pop
: Returns the last item from thearray
and remove itpush
: append a new item (or a list of items)let a = []; a.push(1,2,3); console.log(a);
reduce
,reduceRight
: Return a value as the result of an operation repeated across all elements from the arraylet first = []; for (let i = 0; i < 100; ++i) first.push(i + 1); function sumOfNFirst(n) { return n*(n+1)/2; } let sum = first.reduce(function(accumulator, actual) { return accumulator + actual; }, 0); console.log(sum === sumOfNFirst(100));
reverse
: Return a new array with the order revertedshift
: removes the first or last element from thearray
and returns itslice
: This function gets 2 arguments and returns a newarray
from the first to the last argument. If the last argument is omitted, get all the items from the value passed.let first = []; for (let i = 0; i < 100; ++i) first.push(i + 1); console.log(first.slice(1,5));
some
: Returntrue
if any item pass a conditionsort
: Sort the array in place, in order ascending and with a conversion toUTF-16 char
. If you want to change the sort function, you can pass as an argument a function that gets as arguments 2 consecutive items in thearray.
If the function returns a negative value, it implies that the first argument goes before the second argument. Otherwise, the second argument goes before.let a = [1, 5, 4, 10, 2, 60, 7]; let copy = [1, 5, 4, 10, 2, 60, 7]; let b = a.sort(); //console.log(a) //console.log(a === b); copy.sort(function(a, b) { return a - b }); console.log(copy);
splice
: Edit the content of an array by removing, editing, or adding elements to the originalarray
. This function returns a new array with the removed items.let a = [1, 5, 4, 10, 2, 60, 7]; let b = [1, 5, 4, 10, 2, 60, 7]; let c = [1, 5, 4, 10, 2, 60, 7]; let start = 0, editCount = 0, replaceValue = null; // remove all elements from the start position console.log(a.splice()); console.log(a); // Insert a new item in an arbitrary position console.log(b.splice()); console.log(b); // Remove an item and insert other one console.log(c.splice()) console.log(c);
unshift
: insert a new item as the first position of the array
References:
- JavaScript: The definitive guide : Master the world’s most-used programming language
- MDN Arrays