Talking about the definition of JS multidimensional array and hash array and using _javascript techniques

Source: Internet
Author: User
Tags array definition

Multidimensional array definition

Defines an array object that is used to store a series of values in a separate variable name. 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 multidimensional array definition

JavaScript two-dimensional arrays or multidimensional arrays are modeled by one-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[] [column];

Such as:

ARR[0][0]//A

Arr[1][0]//d

Hash array definition

JavaScript in the associative array, associative array because of the index of key value, so in the array lookup is more convenient, but also make the corresponding code algorithm to achieve a clearer, easy to read and easy to maintain.

var myhash = new Array ();

Add a key value to a hash associative array

myhash[' new ' = ' newval ';

myhash[' new2 '] = ' newval_2 ';

accessing hash associative arrays

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

Delete hash array already has a key value delete myhash[' new '];

Traversal Hash Array

For (key in Myhash) {  
console.log (key);//key get key name  
Myhash[key];//Get Value
}

Common methods for JS array operation

ToString (): Converts an array to a string

toLocaleString (): Converts an array to a string

Join (): Converts an array into a symbolic concatenated string

Shift (): Moves an element of an array's head out

Unshift (): Inserts an element in the header of an array

Pop (): Deletes an element from the tail of an array

Push (): Adds an element to the tail of the array

Concat (): Adding elements to an array

Slice (): Returns the part of an array

Reverse (): Sort the array in reverse order

Sort (): Sorting operations on array

Splice (): Inserts, deletes, or replaces an array element

The above article on the JS multidimensional array and hash array definition and use is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.