Home > Web Front-end > JS Tutorial > A brief discussion on the definition and use of js multi-dimensional arrays and hash arrays

A brief discussion on the definition and use of js multi-dimensional arrays and hash arrays

高洛峰
Release: 2017-01-14 10:50:59
Original
1277 people have browsed it

Multidimensional array definition

Definition of array Array objects are used to store a series of values ​​in separate variable names. Use the keyword new to create an array object.

One-dimensional array definition

var myArray=new Array('a','b','c'); or var myArray = [];

Two-dimensional Array and multi-dimensional array definition

Javascript two-dimensional array or multi-dimensional array are simulated by one-dimensional array.

Method 1.

var arr= new Array(['a','b','c'],['d','e','f']);
Copy after login

Method 2:

var arr=new  Array(
 
 new  Array(), 
 
 new  Array(),
 
 new  Array() 
 
);
Copy after login

Array access:

arr[row][column];

For example:

arr[0 ][0] // a

arr[1][0] //d

hash array definition

Associative array in JavaScript, associative array has key value Index, so it is more convenient in array search, and it also makes the corresponding code algorithm implementation clearer, easier to read and maintain.

var myhash = new Array();
Copy after login

Add key value to Hash associative array

myhash['new'] = 'newval';
 
myhash['new2'] = 'newval_2';
Copy after login

Access Hash associative array

myhash['new']; // 跟上键名就能访问
Copy after login

Delete existing key value in Hash array delete myhash['new'];

Traverse Hash Array

for(key in myhash){ 
console.log(key); //key 获取的是键名 
myhash[key]; // 获取值
}
Copy after login

Common methods for js array operations

toString(): Convert the array into a string

toLocaleString(): Convert the array into A string

join(): Convert the array into a symbolically connected string

shift(): Remove an element from the head of the array

unshift( ): Insert an element at the head of the array

pop(): Delete an element from the tail of the array

push(): Add an element to the tail of the array

concat(): Add elements to the array

slice(): Return part of the array

reverse(): Reverse sort the array

sort(): Sort the array Sorting operation

splice(): Insert, delete or replace an array element

The above article briefly discusses the definition and use of js multi-dimensional arrays and hash arrays is all the content shared by the editor. , I hope it can give everyone a reference, and I also hope everyone will support the PHP Chinese website.

For more articles on the definition and use of js multi-dimensional arrays and hash arrays, please pay attention to 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