First, let’s look at the classification of arrays:
From the subscript of the array, it is divided into index array and associative array
/* 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.
Statement: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