Comparison of general and associative arrays of JavaScript _javascript tips

Source: Internet
Author: User
Tags in python
First, let's look at the array classification:

From the subscript of an array into an indexed array, an associative array
Copy Code code as follows:

/* Index array, that is, an array that is usually called/
var ary1 = [1,3,5,8];
Array elements are indexed by index, starting with 0 (of course some language implementations start with 1), the index is actually ordinal, an integer number
ARY1[0];
ARY1[1];
ARY1[2];
ARY1[3];

/* Associative array, an array that is accessed as subscript with a non-ordinal type called a dictionary in Python
var ary2 = {}; Access to non ordinal (number), here is the string
ary2["one"] = 1;
Ary2["two"] = 2;
ary2["THR"] = 3;
ary2["fou"] = 4;


Question: You can test the following:
alert (arry1.length); The return value is: 4
alert (arry2.length); The return value is: 0

Because JS is a non-type language, so any type of JS is Object,

For example, var arr = [];
Arr[0] = 1;
ARR[1] = 2;
ARR[2] = 3;
Arr["s"] = 4;
At this point you call Arr.s is equivalent to calling arr This object's S property, but this is Arr.length is still 3.

Summary: Associative arrays can be subscript with strings, but this subscript is not a parameter-passing value, in other words, you need to take what you want, sounds very intelligent, in fact, you still need to take the value of the manual to write the subscript.

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.