Home > Article > Web Front-end > Summarize the usage of Array method in js
The content of this article is about summarizing the usage of the Array method in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Some methods of JS Array are very commonly used in practice. They are compiled and recorded here, firstly for frequent review, and secondly for everyone’s convenience
Map
map(): Returns a new Array, each element is the result of calling function
Syntax: array.map(function(currentValue,index,arr), thisValue)
Example:
var numbers = [65, 44, 12, 4], changedValue; function multiplyArrayElement(num) { return num * 2; } (function myFunction() { changedValue = numbers.map(multiplyArrayElement); })() console.log(changedValue);
Filter
var ages = [32, 33, 16, 40], changedValue; function checkAdult(age) { return age >= 18; } (function myFunction() { changedValue = ages.filter(checkAdult); })() console.log(changedValue);
Somesome(): Returns a boolean, Determine whether any element meets the function condition
var ages = [3, 10, 18, 20], changedValue; function checkAdult(age) { return age >= 18; } (function myFunction() { changedValue = ages.some(checkAdult); })() console.log(changedValue);
Every every(): Returns a boolean to determine whether each element meets the function condition
var ages = [32, 33, 16, 40], changedValue; function checkAdult(age) { return age >= 18; } (function myFunction() { changedValue = ages.every(checkAdult); })() console.log(changedValue);
ForEachforEach(): No return value, just call function for each element
var numbers = [4, 9, 16, 25],changedValue; function myFunction(item, index, arr) { arr[index] = item + 1; } numbers.forEach(myFunction); console.log(numbers);Related recommendations:
Summary of Array object method attributes in javacsript
Summary of array and string methods in javascript_Basic knowledge
The above is the detailed content of Summarize the usage of Array method in js. For more information, please follow other related articles on the PHP Chinese website!