Original text| https://javascript.plainenglish.io/8-javascript-array-methods-you-should-know-81947c9e46de
Yang Xiaoer
Arrays form part of almost any programming language. Understanding arrays is important for revealing programming concepts.
According to Wikipedia, an array can be defined as a data structure composed of a set of elements, and each element is identified by at least one array index or key. Stores an array so that the position of each element can be calculated mathematically from its index tuple.
In this article, we'll look at JavaScript array methods and how to use them.
Simply put, an array is just a variable that can hold multiple values at a given time.
It should also be noted that almost all mainstream browsers support these array methods.
1. map() method
The map() method is an array method that creates an array by calling a function on each element that exists in the parent array.
This method does not execute functions for array elements without values.
The map() array method behaves like a pure function and does not change the original array.
example:
const users = [ {name:'John', age:33}, {name:'Philip', age:40}, {name:'Carl', age:30}, {name:'Frank', age:27}, {name:'Florin', age:25}, {name:'Debby', age:21}, {name:'Liam', age:26} ] const userAge=users.map((user) => { return user.name }) console.log(userAge)
This returns all names in the given array.
2. filter() method
The filter() array method returns a given array that passes the given calculation from the original array. Inside filter(), we provide the following functions.
The filter() function cannot be used on array elements without values.
The filter() function does not change the original array, so it behaves like a pure function.
example:
const users = [ {name:'John', age:33}, {name:'Philip', age:40}, {name:'Carl', age:30}, {name:'Frank', age:27}, {name:'Florin', age:25}, {name:'Debby', age:21}, {name:'Liam', age:26} ] const filterUsers = users.filter((user)=>{ return user.age <=27 }) console.log(filterUsers)
3. find() method
The find() array method is used to find the given object in the array.
This method returns the value of the first element that passes the test for an attributive sentence.
This method executes the given function once for each element present in the array.
The find() method does not change the original array provided.
example:
const users = [ {name:'John', age:33}, {name:'Philip', age:40}, {name:'Carl', age:30}, {name:'Frank', age:27}, {name:'Florin', age:25}, {name:'Debby', age:21}, {name:'Liam', age:26} ] const findUser= users.find((user)=>{ return user.name === 'Debby' }) console.log(findUser)
4. forEach() method
The forEach() array method is used to call a specific function for each element in the array.
example:
const users = [ {name:'John', age:33}, {name:'Philip', age:40}, {name:'Carl', age:30}, {name:'Frank', age:27}, {name:'Florin', age:25}, {name:'Debby', age:21}, {name:'Liam', age:26} ] users.forEach((user)=>{ console.log(user.name) })
This function returns all the names in the given array. In most cases, the forEach() method makes it easy to work with arrays.
If you want to terminate an element forEach and execute the forEach of the next element, you can't use continue. You need to use return false
5. some() method
The some() array method is used to check whether a given set of elements in the array pass a specific test.
The some() array method behaves like a pure function.
If a value is passed, it returns true (and does not check the remaining values), otherwise it returns false.
example:
const users = [ {name:'John', age:33}, {name:'Philip', age:40}, {name:'Carl', age:30}, {name:'Frank', age:27}, {name:'Florin', age:25}, {name:'Debby', age:21}, {name:'Liam', age:26} ] const midUsers = users.some((user)=>{ return user.name <= 27 }) console.log(midUsers)
6. every() method
The every() method executes and checks whether all elements in the given array pass the provided test.
It behaves like a pure function and does not change the original array.
example:
const users = [ {name:'John', age:33}, {name:'Philip', age:40}, {name:'Carl', age:30}, {name:'Frank', age:27}, {name:'Florin', age:25}, {name:'Debby', age:21}, {name:'Liam', age:26} ] const everyUsers = users.every((user)=>{ return user.name <= 25 }) console.log(everyUsers)
7. reduce() method
As its name implies, it simplifies the original array into a pair of values and executes the given function for each given value.
example:
const users = [ {name:'John', age:33}, {name:'Philip', age:40}, {name:'Carl', age:30}, {name:'Frank', age:27}, {name:'Florin', age:25}, {name:'Debby', age:21}, {name:'Liam', age:26} ] const totalAge= users.reduce((curr, user)=>{ return user.age + curr }, 0) console.log(totalAge)
8. Include() method
The include () array method checks whether the array contains the given element.
example:
const ages = [19,56,45,54,30,32,21,33,21,18,23,23] const hasTwentyThree=ages.includes(23)
Returns true if the given test passes, otherwise false.