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

js array method to loop through all elements in the array

PHPz
Release: 2018-09-30 16:04:40
Original
1180 people have browsed it

The simplest way to traverse arrays in js is to use for and then use the length of arr.length as the maximum value of for. Let's take a look at some useful examples

Example: for (){} Traverse 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

Example: for in loop traverse 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

The above is the entire content of this chapter. For more related tutorials, please visit the JavaScript video tutorial !

Related labels:
source:php.cn
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