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

Several examples of problems with one-dimensional arrays and two-dimensional arrays in js_Basic knowledge

WBOY
Release: 2016-05-16 16:41:45
Original
1148 people have browsed it

Arrays in js can store various data types (numeric values, strings)

The array in js does not cross the boundary. When the output array subscript crosses the boundary, undefined will be displayed.

Arrays in js grow dynamically by default

A simple way to iterate over an array.

for(var key in arr){
window.alert(key+"= "+arr[key]);
}
Copy after login

Problems that occur when assigning values ​​to an empty two-dimensional array:

var arr2=[];
arr2[1][1]=45;//js不支持这种赋值方法
Copy after login

Solution:

//在这之前需要初始化定义arr2有多少行。
for(var i=0;i<5;i++){
arr2[i]=[];
}
//这样就能对它赋值了。
arr2[1][1]=45;
Copy after login
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