Comparison between regular arrays and associated arrays in Javascript

Source: Internet
Author: User
First, let's take a look at the array classification: from the array subscript into an index array, associated array/* index array, that is, the commonly referred array */varary1 [,]; // retrieve array elements by index, starting from 0 (of course some languages start from 1). The index is actually in order... syntaxHighlighter. all ();

First, let's look at the array classification:

Index Array and associated array by array subscript
/* Index array, which is usually an array */
Var ary1 = [1, 3, 5, 8];
// Retrieve array elements by index, starting from 0 (of course some languages start from 1). The index is actually a ordinal number, an integer number.
Ary1 [0];
Ary1 [1];
Ary1 [2];
Ary1 [3];
 
/* Associated array, which is an array that is accessed by subscripts of the non-ordinal number type. In python, it is called a dictionary */
Var ary2 = {}; // The value is a non-ordinal number (number). Here it is a string.
Ary2 ["one"] = 1;
Ary2 ["two"] = 2;
Ary2 ["thr"] = 3;
Ary2 ["fou"] = 4;

 

Question: You can test it as follows:

Alert (arry1.length); // return value: 4

 

Alert (arry2.length); // return value: 0

 

What is this? Because js is a weak type language, all js types are objects,

For example, var arr = [];
Arr [0] = 1;
Arr [1] = 2;
Arr [2] = 3;
Arr ["s"] = 4;
At this time, you call arr. s, which is equivalent to calling the s attribute of the arr object, but this is arr. length, which is still 3.

Summary: although the correlated array can be used as a subscript by a string, this subscript does not support parameter value passing. In other words, it sounds intelligent to take whatever you need, in fact, you still need to manually write down the value.

 

 

From waiting for apricot on the wall

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.