This content originally appeared on DEV Community and was authored by Clean Code Studio
14 JS Array Methods (In 8 Minutes) Screencast
let stocks = [
{ name: 'Apple', price: 321.85 },
{ name: 'Tesla', price: 2471.04 },
{ name: 'Disney', price: 118.77 },
{ name: 'Google', price: 1434.87 },
{ name: 'Netflix', price: 425.92 }
]
Array.filter
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
stocks.filter(stock => stock.price < 1000)
/*-------------------------------------------
| Array.filter
*-------------------------------------------
| 0: {name: "Apple", price: 321.85}
| 1: {name: "Disney", price: 118.77}
| 2: {name: "Netflix", price: 425.92}
*/
Array.map
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
stocks.map(stock => [stock.name, stock.price])
/*-------------------------------------------
| Array.map
*-------------------------------------------
| 0: (2) ["Apple", 321.85]
| 1: (2) ["Tesla", 2471.04]
| 2: (2) ["Disney", 118.77]
| 3: (2) ["Google", 1434.87]
| 4: (2) ["Netflix", 425.92]
*/
Array.find
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
stocks.find(stock => stock.name === 'Tesla')
/*-------------------------------------------
| Array.find
*-------------------------------------------
| {name: "Tesla", price: 2471.04}
*/
Array.some
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
stocks.some(stock => stock.price < 1000)
/*-------------------------------------------
| Array.some
*-------------------------------------------
| true
*/
stocks.some(stock => stock.price < 10)
/*-------------------------------------------
| Array.some
*-------------------------------------------
| false
*/
Array.every
The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
stocks.every(stock => stock.price < 1000)
/*-------------------------------------------
| Array.every
*-------------------------------------------
| false
*/
stocks.every(stock => stock.price < 2500)
/*-------------------------------------------
| Array.every
*-------------------------------------------
| true
*/
Array.forEach
The forEach() method executes a provided function once for each array element.
stocks.forEach(stock => console.log(stock))
/*-------------------------------------------
| Array.forEach
*-------------------------------------------
| Outputs each item (stock object) from the array to the console
| returns void (aka undefined)
*/
Array.reduce
The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
stocks.reduce((total, stock) => total + stock.price, 0)
/*-------------------------------------------
| Array.reduce
*-------------------------------------------
| 4772.45
*/
let names = ['Apple', 'Tesla', 'Disney', 'Google', 'Netflix']
Array.includes
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
names.includes('Apple')
/*-------------------------------------------
| Array.includes
*-------------------------------------------
| true
*/
names.includes('Microsoft')
/*-------------------------------------------
| Array.includes
*-------------------------------------------
| false
*/
Array.indexOf
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
names.indexOf('Tesla')
/*-------------------------------------------
| Array.indexOf
*-------------------------------------------
| 1
*/
names = ['Apple', 'Tesla', 'Disney', 'Google', 'Netflix', 'Tesla']
Array.lastIndexOf
The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
names.lastIndexOf('Tesla')
/*-------------------------------------------
| Array.lastIndexOf
*-------------------------------------------
| 5
*/
Array.sort
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 time and space complexity of the sort cannot be guaranteed as it depends on the implementation.
names.sort()
/*-------------------------------------------
| Array.sort
*-------------------------------------------
| ['Apple, 'Disney', 'Google', 'Netflix', 'Tesla', 'Tesla']
*/
Array.slice
The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
names.slice(3)
/*-------------------------------------------
| Array.slice
*-------------------------------------------
| ['Netflix', 'Tesla', 'Tesla']
|
*/
Array.join
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.
names.join()
/*-------------------------------------------
| Array.join
*-------------------------------------------
| "Apple,Disney,Google,Netflix,Tesla,Tesla"
*/
names.join(' - ')
/*-------------------------------------------
| Array.join
*-------------------------------------------
| "Apple - Disney - Google - Netflix - Tesla - Tesla"
*/
names.join('\\')
/*-------------------------------------------
| Array.join
*-------------------------------------------
| "Apple\Disney\Google\Netflix\Tesla\Tesla"
*/
Array.toString
The toString() method returns a string representing the specified array and its elements.
names.toString()
/*-------------------------------------------
| Array.toString
*-------------------------------------------
| "Apple,Disney,Google,Netflix,Tesla,Tesla"
*/
stocks = [
{ name: 'Apple', price: 321.85 },
{ name: 'Tesla', price: 2471.04 },
{ name: 'Disney', price: 118.77 },
{ name: 'Google', price: 1434.87 },
{ name: 'Netflix', price: 425.92 }
]
stocks.toString()
/*-------------------------------------------
| Array.toString
*-------------------------------------------
| "[object Object],[object Object],[object Object],[object Object],[object Object]""
*/
14 JS Array Methods (In 8 Minutes) Screencast
Clean Code
Simplify Developer Lives
This content originally appeared on DEV Community and was authored by Clean Code Studio
Clean Code Studio | Sciencx (2021-07-13T20:17:06+00:00) 14 JavaScript Array Methods (In 8 Minutes). Retrieved from https://www.scien.cx/2021/07/13/14-javascript-array-methods-in-8-minutes/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.