-
The
find()
method returns the value of the first element in the provided array that satisfies the provided testing function.const inventory = [ {name: 'apples', quantity: 2}, {name: 'bananas', quantity: 0}, {name: 'cherries', quantity: 5} ]; const result = inventory.find( ({ name }) => name === 'cherries' ); console.log(result) // { name: 'cherries', quantity: 5 }
-
Creates a new array
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const result = words.filter((word) => word.length > 6) console.log(result) // expected output: [ 'exuberant', 'destruction', 'present' ]
-
The
indexOf()
method returns the first index at which a given element can be found in the array or-1
if it is not presentconst beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; console.log(beasts.indexOf('bison')); // expected output: 1 // start from index 2 console.log(beasts.indexOf('bison', 2)); // expected output: 4 console.log(beasts.indexOf('giraffe')); // expected output: -1
-
The
includes()
method determines whether an array includes a certain value among its entries, returning true or false as appropriate.let arr = ['a', 'b', 'c'] arr.includes('c', 3) // false arr.includes('c', 100) // false
-
Creates a new array
const numbers = [1,2,3,4] const map1 = numbers.map((number) => number*2) console.log(map1) // expected output: [ 2, 4, 6, 8 ]
-
The
forEach()
method executes a provided fucntion once for each array elementconst array = ['a', 'b', 'c']; array.forEach((element) => console.log(element)); // expected output: "a" // expected output: "b" // expected output: "c"
-
The
sort()
method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values -
The sorted array. Note that the array is sorted in place, and no copy is made.
const months = ['March', 'Jan', 'Feb', 'Dec']; months.sort(); console.log(months); // expected output: Array ["Dec", "Feb", "Jan", "March"] const array1 = [1, 30, 4, 21, 100000]; array1.sort(); console.log(array1); // expected output: Array [1, 100000, 21, 30, 4]
var items = [ { name: 'Edward', value: 21 }, { name: 'Sharpe', value: 37 }, { name: 'And', value: 45 }, { name: 'The', value: -12 }, { name: 'Magnetic', value: 13 }, { name: 'Zeros', value: 37 } ]; // sort by value items.sort(function(a, b) { return a.value - b.value; }); console.log(items); // [ // { name: 'The', value: -12 }, // { name: 'Magnetic', value: 13 }, // { name: 'Edward', value: 21 }, // { name: 'Sharpe', value: 37 }, // { name: 'Zeros', value: 37 }, // { name: 'And', value: 45 } // ] // sort by name items.sort(function(a, b) { var nameA = a.name.toUpperCase(); // ignore upper and lowercase var nameB = b.name.toUpperCase(); // ignore upper and lowercase if (nameA < nameB) { return -1; } if (nameA > nameB) { return 1; } // names must be equal return 0; }); console.log(items); // [ // { name: 'And', value: 45 }, // { name: 'Edward', value: 21 }, // { name: 'Magnetic', value: 13 }, // { name: 'Sharpe', value: 37 }, // { name: 'The', value: -12 }, // { name: 'Zeros', value: 37 } // ]
-
The
push()
method adds one or more elements to the end of an array and returns the new length of the array.let sports = ['soccer', 'baseball'] let total = sports.push('football', 'swimming') console.log(sports) // ['soccer', 'baseball', 'football', 'swimming'] console.log(total) // 4
-
The
unshift()
method adds one or more elements to the beginning of an array and returns the new length of the array.let arr = [1, 2] arr.unshift(0) // result of the call is 3, which is the new array length // arr is [0, 1, 2] arr.unshift(-2, -1) // the new array length is 5 // arr is [-2, -1, 0, 1, 2] arr.unshift([-4, -3]) // the new array length is 6 // arr is [[-4, -3], -2, -1, 0, 1, 2] arr.unshift([-7, -6], [-5]) // the new array length is 8 // arr is [ [-7, -6], [-5], [-4, -3], -2, -1, 0, 1, 2 ]
-
The
pop()
method removes the last element from an array and returns that element. This method changes the length of the array.let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; let popped = myFish.pop(); console.log(myFish); // ['angel', 'clown', 'mandarin' ] console.log(popped); // 'sturgeon'
-
The
shift()
method removes the first element from an array and returns that removed element. This method changes the length of the array.let myFish = ['angel', 'clown', 'mandarin', 'surgeon']; console.log('myFish before:', JSON.stringify(myFish)); // myFish before: ['angel', 'clown', 'mandarin', 'surgeon'] let shifted = myFish.shift(); console.log('myFish after:', myFish); // myFish after: ['clown', 'mandarin', 'surgeon'] console.log('Removed this element:', shifted); // Removed this element: angel
-
The
slice()
method returns a shallow copy of a portion of the array into a new array object selected frombegin
toend
(end
not included) wherebegin
andend
represent the index of items in the array. The original array will not be modified.const animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; console.log(animals.slice(2)); // expected output: Array ["camel", "duck", "elephant"] console.log(animals.slice(2, 4)); // expected output: Array ["camel", "duck"] console.log(animals.slice(1, 5)); // expected output: Array ["bison", "camel", "duck", "elephant"]
-
The
splice()
method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.const months = ['Jan', 'March', 'April', 'June']; months.splice(1, 0, 'Feb'); // inserts at index 1 console.log(months); // expected output: Array ["Jan", "Feb", "March", "April", "June"] months.splice(4, 1, 'May'); // replaces 1 element at index 4 console.log(months); // expected output: Array ["Jan", "Feb", "March", "April", "May"] months.splice(2, 1); // removes one element at index 2 console.log(months); // expected output: Array [ 'Jan', 'Feb', 'April', 'May' ]
-
The
join()
method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.let a = ['Wind', 'Water', 'Fire']; a.join(); // 'Wind,Water,Fire' a.join(', '); // 'Wind, Water, Fire' a.join(' + '); // 'Wind + Water + Fire' a.join(''); // 'WindWaterFire'
-
The
keys()
method returns a new Array Iterator object that contains the keys for each index in the array.const array1 = ['a', 'b', 'c']; const iterator = array1.keys(); for (const key of iterator) { console.log(key); } // expected output: 0 // expected output: 1 // expected output: 2
let arr = ['a', , 'c']; let sparseKeys = Object.keys(arr); let denseKeys = [...arr.keys()]; console.log(sparseKeys); // ['0', '2'] console.log(denseKeys); // [0, 1, 2]
-
The
values()
method returns a new Array Iterator object that contains the values for each index in the array. -
Iteration using for...of loop
var arr = ['a', 'b', 'c', 'd', 'e']; var iterator = arr.values(); for (let letter of iterator) { console.log(letter); } //"a" "b" "c" "d"
-
The
reverse()
method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.const a = [1, 2, 3]; console.log(a); // [1, 2, 3] a.reverse(); console.log(a); // [3, 2, 1]
-
The
toString()
method returns a string representing the specified array and its elements.const array1 = [1, 2, 'a', '1a']; console.log(array1.toString()); // expected output: "1,2,a,1a"