(Original Array) const fruits = ["Banana", "Orange", "Apple", "Mango"] | |||
---|---|---|---|
Example | returns | Modified Array | Purpose |
fruits.copyWithin(2, 0); | ["Banana", "Orange", "Banana", "Orange"] | copies array elements to another position in an array, overwriting the existing values. | |
fruits.fill("Kiwi"); | ["kiwi", "kiwi", "kiwi", "kiwi"] | fills specified elements in an array with a static value. | |
fruits.push("Kiwi"); | 5 | ["Banana", "Orange", "Apple", "Mango", "Kiwi"] | adds new items to the end of an array. |
fruits.unshift("Lemon","Pineapple"); | 6 | [“Lemon”, “Pineapple”, "Banana", "Orange", "Apple", "Mango"]; | adds new items to the beginning of an array, and returns the new length. |
fruits.splice(2, 0, "Lemon", "Kiwi"); | ["Banana", "Orange", “Lemon”, “Kiwi”, "Apple", "Mango"]; | adds and/or removes array elements. | |
fruits.pop(); | "Mango" | ["Banana", "Orange", "Apple"]; | removes the last element of an array |
fruits.shift(); | "Banana" | ["Orange", "Apple", "Mango"]; | removes the first item of an array. |
fruits.sort(); | ["Apple", "Banana", "Mango", "Orange" ]; | sorts the elements of an array. | |
fruits.reverse(); | ["Mango", "Apple", "Orange", "Banana"] | reverses the order of the elements in an array. |
Example | New Array(returned) | Original Array | Purpose | function |
---|---|---|---|---|
hege.concat(stale); | ["Emil", "Tobias", "Linus", "Cecilie", "Lone"]; | const hege = ["Cecilie", "Lone"]; const stale = ["Emil", "Tobias", "Linus"]; | joins two or more arrays. | |
Array.from("ABCDEFG"); | [A,B,C,D,E,F,G] | "ABCDEFG" | Create an Array from a String | |
fruits.join() | "Banana,Orange,Apple,Mango" | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | returns an array as a string. It behaves just like toString, but you can also specify the separator | |
fruits.toString(); | "Banana,Orange,Apple,Mango" | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | returns a string with all array values separated by commas. | |
fruits.slice(1,3); | ["Orange", "Lemon"]; | const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; | returns selected elements in an array, as a new array. | |
numbers.map(Math.sqrt) ; | [2, 3, 4, 5] | const numbers = [4, 9, 16, 25]; | creates a new array with the results of calling a function for every array element. | |
ages.filter(checkAdult); | [32, 33, 40] | const ages = [32, 33, 16, 40]; ; | creates an array filled with all array elements that pass a test (provided by a function). | function checkAdult(age) { return age >= 18; } |
Example | returns | orignal array | Purpose | function |
---|---|---|---|---|
fruits.length; | 4 | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | returns the number of elements in an array | |
fruits.includes("Mango") | //true | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | true if an array contains a specified element, otherwise false. | |
fruits.indexOf("Apple") | 2 | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | searches an array for a specified item and returns its position. | |
Array.isArray(fruits) | //true | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | returns true if an object is an array, otherwise false. | |
fruits.valueOf(); | ["Banana", "Orange", "Apple", "Mango"] | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | returns the array itself. | |
fruits.lastIndexOf("Apple") | 2 | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | returns the last index (position) of a specified value. | |
numbers.reduce(myFunc); | 100 | const numbers = [175, 50, 25]; | executes a reducer function for each value of an array. | function myFunc(total, num) { return total - num; } |
numbers.reduceRight(myFunc) | -200 | const numbers = [175, 50, 25]; | executes a reducer function for each value of an array , from right to left. | function myFunc(total, num) { return total - num; } |
ages.some(checkAdult) | //true | const ages = [3, 10, 18, 20]; | checks if any of the elements in an array pass a test (provided as a function). | function checkAdult(age) { return age >= 18; } |
ages.every(checkAge) | //false | const ages = [32, 33, 16, 40]; | returns true if all elements in an array pass a test (provided as a function). | function checkAge(age) { return age > 18; } |
ages.find(checkAge) | 20 | const ages = [3, 10, 18, 20]; | returns the value of the array element that passes a test (provided by a function). | function checkAge(age) { return age > 18; } |
ages.findIndex(checkAge) | 3 | const ages = [2, 10, 18, 20]; | returns the index of the first array element that passes a test (provided by a function). | function checkAge(age) { return age > 18; } |
fruits.entries(); | [0, "Banana"] [1, "Orange"] [2, "Apple"] [3, "Mango"] | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | returns an Array Iterator object with key/value pairs. | for (let x of f) { document.getElementById("demo").innerHTML += x; } |
fruits.keys() | 0,1,2,3 | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | returns an Array Iterator object with the keys of an array. | for (let x of keys) { text += x + "<br>"; } |
Example | returns | Original Array | Purpose | function |
---|---|---|---|---|
fruits.forEach(myFunction); | 0: apple 1: orange 2: cherry | const fruits = ["apple", "orange", "cherry"]; | calls a function once for each element in an array, in order. | function myFunction(item, index) { text += index + ": " + item + "<br>"; } |
Example | returns | Original Array | Purpose | function |
---|---|---|---|---|
fruits.constructor; | function Array() { [native code] } | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | returns the function that created the Array prototype. | |
fruits.myUcase(); | BANANA,ORANGE,APPLE,MANGO | const fruits = ["Banana", "Orange", "Apple", "Mango"]; | add new properties and methods to Arrays. | Array.prototype.myUcase = function() { for (let i = 0; i < this.length; i++) { this[i] = this[i].toUpperCase(); } }; |