Home > Web Front-end > JS Tutorial > body text

Summarize several methods of traversing arrays in JavaScript

青灯夜游
Release: 2020-07-29 16:58:21
forward
2884 people have browsed it

This article summarizes some methods for traversing arrays in javascript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Summarize several methods of traversing arrays in JavaScript

There are several ways to traverse an array, and they will be listed one by one below!

while loop

let index = 0;
const array = [1, 2, 3, 4, 5];

while (index < array.length) {
    console.log(array[index]);
    index++;
}
Copy after login

Summarize several methods of traversing arrays in JavaScript

for loop

const array = [1,2,3,4,5];
for(let index=0;index
Copy after login

Summarize several methods of traversing arrays in JavaScript

forEach

const array=[1,2,3,4,5];
array.forEach(function(current_value,index,array){
    console.log(`At index ${index} in array ${array} the value is ${current_value}`)
})
Copy after login

Summarize several methods of traversing arrays in JavaScript

map

The last construct is useful, but does not return a new array, which may be undesirable for your particular case. map solves this problem by applying a function to each element and then returning a new array.

const array = [1,2,3,4,5];
const square = x =>Math.pow(x,2);
const squares = array.map(square);
console.log(`${array}`);
console.log(`${squares}`)
Copy after login

Summarize several methods of traversing arrays in JavaScript

reduce

The reduce() method applies a function to the accumulator and each element in the array (from left to right) to Reduce to a single value

const array = [1,2,3,4,5];
const sum = (x,y) => x + y;

const array_sum = array.reduce(sum,0);
console.log(`the sum of arrray:${array} is ${array_sum}`);
Copy after login

Summarize several methods of traversing arrays in JavaScript

filter

Filter elements in the array based on a Boolean function

const array = [1,2,3,4,5];
const even = x => x%2 === 0;
const even_array = array.filter(even);
console.log(`even numbers in array ${array} : ${even_array}`);
Copy after login

Summarize several methods of traversing arrays in JavaScript

every

I got an array and wanted to test whether each element meets the given condition

const array = [1,2,3,4,5,8];
const under_six = x => x<6;
if(array.every(under_six)){
    console.log(`every elemnet in the array is less than 6`);
}
else{
    console.log(`at least one element in the array was bigger than 6`);
}
Copy after login

Summarize several methods of traversing arrays in JavaScript

some

Test whether At least one element matches the Boolean function

const array = [2,4,5,6,8];
const over_five = x => x>5;

if(array.some(over_five)){
    console.log(`at least one element bigger than 5 was found`);
}
else{
    console.log(`no element bigger than 5 was found`);
}
Copy after login

Summarize several methods of traversing arrays in JavaScript

That’s it. If you have anything else, please add it!

Recommended related tutorials: JavaScript video tutorial

The above is the detailed content of Summarize several methods of traversing arrays in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!