The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects.
Array.from
Creates a new, shallow-copied Array instance from an array-like or iterable object.
1 | let squares = Array.from(document.querySelectorAll('.grid div')); |
concat
Merge two arrays together.
1 | var planets = ['Mars']; |
every
The every()
method tests whether all elements in the array pass the test implemented by the provided function.
fill
1 | this.state = { |
forEach
The inbuilt forEach()
method executes a provided function once for each array element.
1 | let names = ['foo', 'bar']; |
some
The some()
method tests whether at least one element in the array passes the test implemented by the provided function.
So for the below if any of the the functions functionA
or functionB
return true then the result is true.
1 | itemArray.some(functionA, functionB); |
slice
Call .slice()
to create a copy of the squares array to modify instead of modifying the existing array.
1 | const squares = this.state.squares.slice(); |
- https://www.w3schools.com/jsref/jsref_slice_array.asp
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
splice
Mutates an array by removing items/items in an array and replace them with new ones. Usage is itemArray.splice(startIndex, deleteCount, [NEW ARRAY ITEMS])
Example to delete items
1 | var planets = ['Mars', 'Saturn', 'Pluto', 'Earth']; |
Example to add items
1 | var planets = ['Mars', 'Saturn', 'Earth']; |
Example to remove all after an index
1 | var planets = ['Mars', 'Pluto', 'Uranus', 'Saturn', 'Earth']; |
length
The Array.prototype.length property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
1 | const arr = ['foo', 'bar', 'baz']; |