Home > Web Front-end > JS Tutorial > Comparison summary of regular arrays and associative arrays in Javascript_javascript skills

Comparison summary of regular arrays and associative arrays in Javascript_javascript skills

WBOY
Release: 2016-05-16 17:53:13
Original
1358 people have browsed it

First, let’s look at the classification of arrays:

From the subscript of the array, it is divided into index array and associative array

Copy code The code is as follows:

/* Index array, which is usually called an array*/
var ary1 = [1,3,5,8];
// Get the array elements by index, starting from 0 (of course some languages ​​start from 1). The index is actually an ordinal number, an integer number
ary1[0];
ary1[1];
ary1[2];
ary1[3];

/* Associative array refers to an array accessed with a non-ordinal type as a subscript. It is called a dictionary in Python*/
var ary2 = {}; //When accessing, use non-ordinal numbers (numbers), here is the string
ary2["one"] = 1;
ary2["two"] = 2;
ary2[" thr"] = 3;
ary2["fou"] = 4;


Question: You can test as follows:
alert(arry1.length); // return The value is: 4
alert(arry2.length); //The return value is: 0

What is the reason for this? Because js is a non-type language, any type of js is object,

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

Summary: Although associative arrays can use strings as subscripts, this subscript does not support parameter value transfer. In other words, you can get whatever you need. It sounds smart, but in fact you get the value. You still need to write the bid manually.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template