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

How to traverse an array in jquery

王林
Release: 2020-11-17 09:51:31
Original
5783 people have browsed it

Jquery method of traversing an array: You can use a for loop to traverse the array, such as [for(var i=0;i

How to traverse an array in jquery

The method of traversing the array is as follows:

(Learning video recommendation: javascript video tutorial)

1. for loop

var arr = new Array(13.5,3,4,5,6);
for(var i=0;i<arr.length;i++){
 arr[i] = arr[i]/2.0;
}
Copy after login

2. for,in loop

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

3.each traverses one-dimensional array

var arr=new Array();
arr=["aaa","bbb","ccc"];
$.each(arr,function(index,value){
     alert(i+"..."+value);
});
Copy after login

4.each traverses two Dimensional array

$(function () {
    $.each([["aaa", "bbb", "ccc"], ["ddd", "eee", "fff"], ["ggg", "hhh", "iii"]], function (index, item) {
         alert(index + "..." + item);
         //输出0...aaa,bbb,ccc  1...ddd,eee,fff  2...ggg,hhh,iii   这时的index为数组下标,item相当于取这二维数组中的每一个数组
         $.each(item, function (index, itemobj) {
              alert(index + "....." + itemobj);
         });
    });
     //输出0...aaa,bbb,ccc  0...aaa 1...bbb 2...cccc  1...ddd,eee,fff  0...ddd 1...eee 2...fff  2...ggg,hhh,iii 0...ggg 1...hhh 2...iii
 });
Copy after login

Related recommendations:js tutorial

The above is the detailed content of How to traverse an array in jquery. For more information, please follow other related articles on the PHP Chinese website!

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