Introduction to js multi-dimensional array and hash array definition and use

Source: Internet
Author: User
Tags array definition
The following small series will introduce the definition and use of js multi-dimensional arrays and hash arrays. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at the multi-dimensional array definition.

Define an array object 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 multi-dimensional array Definition

Javascript two-dimensional arrays or multi-dimensional arrays are simulated 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 [row] [column];

For example:

Arr [0] [0] //

Arr [1] [0] // d

Hash array Definition

The associated array in JavaScript is more convenient in array search because the associated array has an index of the key value. It also makes the implementation of the corresponding code algorithm clearer and easier to read and maintain.

var myhash = new Array();

Add a key value to the Hash join Array

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

Access the Hash associated array

Myhash ['new']; // access with the key name

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

Traverse Hash Array

For (key in myhash) {console. log (key); // key obtains the key name myhash [key]; // gets the value}

Common js Array Operations

ToString (): converts an array into a string.

ToLocaleString (): converts an array into a string.

Join (): converts an array into a string connected with symbols.

Shift (): removes an element from the array header.

Unshift (): insert an element in the header of the array.

Pop (): deletes an element from the end of the array.

Push (): Add an element to the end of the array.

Concat (): add elements to the array

Slice (): returns the part of the array

Reverse (): sorts arrays in reverse order.

Sort (): sorts arrays.

Splice (): insert, delete, or replace an array element.

The definition and use of js multi-dimensional arrays and hash arrays are all the content shared by the editor. I hope to give you a reference and support for PHP.

For more information about how to define and use js multi-dimensional arrays and hash arrays, see the PHP Chinese website!

Related Article

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.