1. filter()grammar:
Parameter Description: //Filter elements whose age is greater than 10 var ages = [5, 32, 7, 10, 33, 12, 40]; var res = ages.filter(function (currentValue) { return currentValue > 10; }) console.log(res.toString()); // Output: 32,33,12,40 //Arrow function writing var res1 = ages.filter(item => item > 10) console.log(res.toString()); Output :
2. forEach()grammar:
//Loop and output each parameter var ages = [5, 32, 7, 10, 33, 12, 40]; ages.forEach(function (currentValue, index) { console.log("Parameter:" + currentValue + "Index:" + index); }) //Arrow function writing ages.forEach((item, index) => { console.log("parameter:" + item + "index:" + index); }) Let’s look at the following code: //Change 10 to 20 var ages = [5, 32, 7, 10, 33, 12, 40]; ages.forEach(function (currentValue, index) { if (currentValue === 10) { ages[index] = 20 return } console.log(index); }) console.log(ages); After we changed the value of 10 to 20 in the code, we added a 3. some()grammar:
//Change 10 to 20 var ages = [5, 32, 7, 10, 33, 12, 40]; ages.some(function (currentValue, index) { if (currentValue === 10) { ages[index] = 20 return true } console.log(index); }) console.log(ages); //Change 10 to 20 arrow function var ages = [5, 32, 7, 10, 33, 12, 40]; ages.some((item, index) => { if (item === 10) { ages[index] = 20 return true } console.log(index); }) console.log(ages); The running result of the above code will only print the value of 4. every()grammar:
// Check if the value of each element is greater than 4 var ages = [5, 32, 7, 10, 33, 12, 40]; var res = ages.some(function (currentValue) { return currentValue > 4 }) console.log(res); // Output: true //arrow function var res = ages.some(item => item > 4) console.log(res); 5. reduce()grammar:
Parameter Description: //Calculate the sum of all elements var numbers = [15.5, 2.3, 1.1, 4.7]; var res = numbers.reduce(function (total, currentValue) { return total += currentValue }, 0) console.log(res); //23.6 //Calculate the sum of elements greater than 4 var result = numbers.filter(item => item > 4).reduce((total, item) => total += item, 0) console.log(result); //20.2 6. Merge arrays
var arr = [1, 2, 3] var arr2 = [4, 5, 6] var res = [...arr, ...arr2] console.log(res); // Output: [1, 2, 3, 4, 5, 6] var res = [...arr2, ...arr] console.log(res); // Output: [4, 5, 6, 1, 2, 3] This is the end of this article about commonly used JavaScript array methods. For more relevant array JavaScript content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to limit the number of concurrent connection requests in nginx
>>: Mysql sorting and paging (order by & limit) and existing pitfalls
Table of contents Introduction The following is a...
1. Enter the host machine of the docker container...
Get daily statistics When doing a project, you ne...
First, the HTML code to embed the video in the pag...
Table of contents Typical Cases Appendix: Common ...
This article shares the specific code of JavaScri...
This article example shares the specific code of ...
Table of contents 1. Overview 2. Use Keepalived t...
Project scenario: When running the Vue project, t...
Table of contents 1. How to obtain different view...
1. HTML font color setting In HTML, we use the fo...
Preface The string types of MySQL database are CH...
Table of contents Where are the logs stored? View...
Mirroring is also one of the core components of D...
Preface I recently installed MySQL 5.7 and found ...