Home  >  Article  >  Web Front-end  >  Summarize the usage of Array method in js

Summarize the usage of Array method in js

不言
不言Original
2018-09-11 15:20:241303browse

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

##filter(): Returns an array of elements that meet function conditions

Syntax: array .filter(function(currentValue,index,arr), thisValue)

Example:

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


Syntax: array.some(function(currentValue,index,arr),thisValue)

Example:

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


Syntax: array.every(function(currentValue,index,arr), thisValue)

Example:

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


Syntax: array.forEach(function(currentValue , index, arr), thisValue)

Example:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn