Home  >  Article  >  Web Front-end  >  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

高洛峰
高洛峰Original
2017-01-14 10:50:591217browse

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']);

Method 2:

var arr=new  Array(
 
 new  Array(), 
 
 new  Array(),
 
 new  Array() 
 
);

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();

Add key value to Hash associative array

myhash['new'] = 'newval';
 
myhash['new2'] = 'newval_2';

Access Hash associative array

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

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

Traverse Hash Array

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

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!


Statement:
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