- Creating an Array
- How to Recognize an Array
- Array Properties and Methods
- pop & push
- shift & unshift
Arrays are a special type of objects. The
Never put a comma after the last element (like “BMW”,).
The effect is inconsistent across browsers.
pop() method removes the last element from an array and returns the value that was popped.
push() method adds a new element to an array (at the end), and returns the new array length.
Shifting is equivalent to popping, working on the first element instead of the last.
shift() method removes the first array element and “shifts” all other elements to a lower index, and return the item that is shifted out.
unshift() method adds a new element to an array (at the beginning), and “unshifts” older elements, and return the new length.
splice() can be used to add new elements to Array.
The first parameter (2) defines the position where new elements should be added (spliced in).
The second parameter (0) defines how many elements should be removed.
The rest of the parameters (“Lemon” , “Kiwi”) define the new elements to be added.
Using delete may leave undefined holes in the array. Use pop() or shift() instead.
The first parameter (0) defines the position where new elements should be added (spliced in).
The second parameter (1) defines how many elements should be removed.
The rest of the parameters are omitted. No new elements will be added.
find() method returns the value of the first element in an array that pass a test (provided as a function).
slice() method slices out a piece of an array into a new array. This example slices out a part of an array starting from array element 1 (“Orange”):
slice() method can take two arguments like
The method then selects elements from the start argument, and up to (but not including) the end argument.
If the end argument is omitted, like in the first examples, the
slice() method slices out the rest of the array.
Solution 1 (ES5)
Solution 2 create your own
Solution 3 use