Home Web Front-end JS Tutorial Summary of methods for traversing arrays in JavaScript

Summary of methods for traversing arrays in JavaScript

Nov 09, 2017 pm 02:59 PM
javascript js Summarize

I believe everyone knows the role of arrays. It manages the storage space of multiple data in batches. Arrays have a long data structure, which can greatly improve the execution efficiency of programs, whether in PHP or ## In #JavaScript or in jQuery, the form of arrays is divided into index arrays and associative arrays, so how to traverse the array! Below we will give you a detailed introduction toJavaScript arrayTraversal~

1.for(){}Traversing the array

<script type="text/javascript">
 <!--
var arr = new Array(13.5,3,4,5,6);
for(var i=0;i<arr.length;i++){
 arr[i] = arr[i]/2.0;
}
alert(arr);
 //-->
 </script>
Copy after login

2.for in loop traverses the array

<html>
<body>
<script type="text/javascript">
var x
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (x in mycars)
{
document.write(mycars[x] + "<br />")
}
</script>
</body>
</html>
Copy after login

3.forEach methodNote: the forEach() method is introduced by the ES5.1 standard.

var arr=[1,2,3,4,5,6];
arr.forEach(function(v,i){//v==value 为arr项,i==index 为arr索引
    console.log(i+&#39;  &#39;v );
})//输出0  11  22  33  44  55  6
Copy after login

4. A new method has been added in the latest ES6 standard==for of==

for of method  You can use subscript loops when traversing Array, but you cannot use subscripts when traversing Map and Set. Map, set – no subscript, cannot use index to traverse
In order to unify collection types, the ES6 standard introduces a new iterable type,
Array, Map and Set all belong to iterable types.
==Collections with iterable types can be traversed through the new for ... of loop. ==

var arr=[1,2,3,4,5,6];for(var value of Arr){
    console.log(value);
}  //输出123456
Copy after login

Summary:

The JavaScript method of traversing arrays has been summarized. Do you believe that you are familiar with arrays? We have a certain understanding of traversal , but in our actual development, we still need to choose according to our own needs, because the execution efficiency of some methods is not the best! So use with caution!

Related recommendations:

1.Detailed explanation of the difference between javascript array traversal for and for in

2.php array and js array traversal

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

Summarize the usage of system() function in Linux system Summarize the usage of system() function in Linux system Feb 23, 2024 pm 06:45 PM

Summarize the usage of system() function in Linux system

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

See all articles