On the Definition and Application of js Multidimensional Array and hash Array

  • 2021-07-06 09:38:39
  • OfStack

Multidimensional array definition

Defining Array Array objects are used to store 1 series values in separate variable names. Use the keyword new to create an array object.

1-dimensional array definition

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

Definition of 2-dimensional array and multi-dimensional array

javascript 2-dimensional arrays or multi-dimensional arrays are modeled by 1-dimensional arrays.

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

Such as:

arr[0][0] // a

arr[1][0] //d

hash Array Definition

The associative array in JavaScript, because the associative array has the index of key value, it is convenient to find the array, and at the same time, it makes the corresponding code algorithm appear clearer, easy to read and easy to maintain.


var myhash = new Array();

Add a key value to an Hash associative array


myhash['new'] = 'newval';

myhash['new2'] = 'newval_2';

Access Hash associative array


myhash['new']; //  You can access it by following the key name 

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

Traversing Hash Array


for(key in myhash){  
console.log(key); //key  Gets the key name   
myhash[key]; //  Get a value 
}

Common Methods of js Array Operation

toString (): Converts an array to a string

toLocaleString (): Converts an array to a string

join (): Converts an array to a symbolically concatenated string

shift (): Move 1 element out of the array header

unshift (): Inserts 1 element at the head of an array

pop (): Delete 1 element from the end of array

push (): Add 1 element to the end of the array

concat (): Adding elements to an array

slice (): Returns part of an array

reverse (): Reverse Sort Array

sort (): Sort an array

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


Related articles: