Javascript array details, javascript Array
If you are an experienced developer, you may think that this problem is relatively simple, but sometimes we will feel that this problem is quite interesting.
First, let's take a look at the definition of the array:"An array is just a list of values which can be accessed by using an integer as the" key ". The list starts at 0 and goes up from there ."In the following example, we use an object to describe the definition of an array:
Copy codeThe Code is as follows:
Var arr = ["benjamin", "zuojj"];
// =>
Var arr = {
"0": "benjamin ",
"1": "zuojj"
};
Looking at the example above, I always feel that something is missing, OK, and the length of the array:
Copy codeThe Code is as follows:
Var arr = {
"0": "benjamin ",
"1": "zuojj ",
"Length": 2
};
We know that in Javascript, arrays are special objects. We can access the attributes of objects by accessing arrays. At the same time, Arrays can also be added as objects. See the following example:
Copy codeThe Code is as follows:
Var arr = {
"0": "benjamin ",
"1": "zuojj ",
"Length": 2
};
// Outputs: "benjamin"
Console. log (arr [0]);
// Outputs: 2
Console. log (arr. length );
Var arr = ["benjamin", "zuojj"];
Arr. url = "www.jb51.net ";
// Outputs: "www.jb51.net"
Console. log (arr. url );
// Outputs: 2
Console. log (arr. length );
Next let's take a look at the Array methods. There are many operational methods for arrays, such as indexOf/slice/splice/sort. We know that these methods actually exist in Array. prototype. See the following example:
Copy codeThe Code is as follows:
Var arr = ["benjamin", "zuojj"];
// Outputs: 1
Console. log (arr. indexOf ("zuojj "));
Arr. indexOf = function (str ){
Return "It is med indexOf! ";
}
// Outputs: "It is med indexOf! "
Console. log (arr. indexOf ("zuojj "));
In fact, we can use the object to overload all array methods. Let's look at the example of the push method below:
Copy codeThe Code is as follows:
Var arr = {
Length: 0,
Push: function (val ){
// Assign a value
This [this. length] = val;
// Update the array Length
This. length + = 1;
// Returns the length of the array.
Return this. length;
}
}
Arr. push ("zuojj ");
Arr. push ("benjamin ");
// Object {0: "zuojj", 1: "benjamin", length: 2, push: function}
Console. log (arr );
However, one cannot be implemented again. The literal definition of an array is as follows:
Copy codeThe Code is as follows: var arr = ["benjamin", "zuojj"];
However, we can use constructors instead:
Copy codeThe Code is as follows: var arr = new Array ("benjamin", "zuojj ");
If the array definition is not applicable to literal expressions, We can redefine the array definition in our own way.
Copy codeThe Code is as follows: var myArr = new CustomArray ("benjamin", "zuojj ");
Now you know how arrays in javascript work. I hope it will help you.
How does javascript obtain the number of group members in the array?
Var arr = [];
Alert (arr. length );
In JavaScript, how does one define a one-dimensional Array?
String str = ""; // define a String
Str = "He eats no fish and plays the qame"; // value assignment
String [] A = str. split (""); // group by Space
The result is A [0] = He A [1] = eats A [2] = no A [3] = fish A [4] = and A [5] = palys [6] = theA [7] = qame
The split () method separates strings into arrays by specified characters!