The array method is used in JavaScript to summarize
- 2020-12-21 17:58:59
- OfStack
Define an array
Var arryMap = {riskId: " <%=riskid%> " ,riskType: " <%=risktype%> " };
or
Var arry =[];
Use:
var risk = arryMap.riskId;
Arry.push({id: " 1 " ,name: " 1 " });
Arry.push({id: " 2 " ,name: " 2 " });
You can also put a single value in an array
Such as:
var data2 = [];
Data2.push(1);
Data2.push(2);
Loop array value
Var data1 =[2];
data1 = arry;
If(data1 !=null){
For(var i in last){
Var lat = last[i];
Alert(lat.id);
Alert(lat.name);
}
}
Output: 1, 1, 2, 2
The use of arrays in js
1. Creation of arrays
var name= new Array(); // create 1 An array
name[0]="zhangsan"; // Assign values to arrays
name[1]="lisi";
var arrayObj = new Array([size]); // create 1 Number of arrays and specify length, not upper bound, but length
var name=["zhangsan","lisi"]; // create 1 An array and an assignment
var name=new Array("zhangsan","lisi");
To be clear, although the second method specifies the length of the array, in all cases the array is variable in length, which means that even if the length is specified as 5, you can still store elements outside the specified length. Note that the length changes accordingly.
2, access to the elements of the array
var testGetArrValue=arrayObj[1]; // Gets the element value of the array
arrayObj[1]= " This is the new value "; // Assign a new value to an array element
3. Add array elements
arrayObj. push([item1 [item2 [. . . [itemN ]]]]);// will 1 Two or more new elements are added to the end of the array and the new length of the array is returned
arrayObj.unshift([item1 [item2 [. . . [itemN ]]]]);// will 1 When one or more new elements are added to the beginning of an array, the elements in the array automatically move back, returning the new length of the array
arrayObj.splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);// will 1 Two or more new elements are inserted into the array at the specified location. The element inserted at the specified location is automatically moved back and returned "" .
4, the deletion of array elements
arrayObj.pop(); // Remove the last 1 Returns the value of the element
arrayObj.shift(); // Remove the former 1 Returns the value of the element, and the elements in the array automatically move forward
arrayObj.splice(deletePos,deleteCount); // Deletes from the specified location deletePos The specified number to start with deleteCount Returns the removed element as an array
5. Interception and merger of arrays
arrayObj.slice(start, [end]); // Returns an array as an array 1 Partial, note not included end The corresponding element, if omitted end Will be copied start All of the elements after that
arrayObj.concat([item1[, item2[, . . . [,itemN]]]]); // Concatenates multiple arrays (which can also be strings, or a mixture of arrays and strings) as 1 Returns a new array that has been concatenated
6. Copy of array
arrayObj.slice(0); // Returns a copy of the array. Note that it is a new array, not a point
arrayObj.concat(); // Returns a copy of the array. Note that it is a new array, not a point
7. Sorting array elements
arrayObj.reverse(); // Reverses the elements (first to last, last to first) to return the array address
arrayObj.sort(); // Sorts array elements and returns array addresses
8. Stringing array elements
arrayObj.join(separator); // Returns a string that will represent each of the arrays 1 The values of the elements are linked in 1 For intermediate use separator Separated.
toLocaleString , toString , valueOf : Can be regarded as join Not in common use
2. Three properties of an array object
1. length attribute
The Length attribute represents the length of the array, that is, the number of elements in it. Because the index of an array always starts at 0, the upper and lower limits of an array are 0 and ES77en-1. Unlike most other languages, the length attribute of the JavaScript array is mutable, which requires special attention. When the length attribute is set larger, the state of the entire array does not actually change, only the length attribute becomes larger. When the length attribute is set smaller than the original, the values of all elements in the original array with indexes greater than or equal to length are lost. Here is an example of changing the length attribute:
var data2 = [];
Data2.push(1);
Data2.push(2);
0
From the above code we can clearly see the nature of the length attribute. But not only can the length object be set explicitly, it can also be modified implicitly. You can use an undeclared variable in JavaScript, or you can use an undefined array element (an element with an index greater than or equal to length), where the value of the length attribute is set to the value of the element to be used plus 1. For example, the following code:
var data2 = [];
Data2.push(1);
Data2.push(2);
1
The code also defines an array of 10 numbers, which is 10 in length, as shown by the alert statement. Then I use the element with an index of 15 and assign it a value of 15, that is, arr[15]=34. Then I use the alert statement to output the length of the array, which gives me 16. However, this is a surprising feature for developers used to strongly typed programming. In fact, the initial length of an array created in the form of new Array() is zero, and it is the manipulation of undefined elements in the array that changes the length of the array.
As you can see from the introduction above, the length attribute is so magical that it is convenient to increase or decrease the size of the array. Therefore, a deep understanding of the length attribute is conducive to flexible application in the development process.
2. prototype attribute
Returns a reference to the object type stereotype. The prototype attribute is common to object.
objectName.prototype
The objectName parameter is the name of the object object.
Description: Use the prototype attribute to provide a basic set of functions for the class of the object. A new instance of an object "inherits" the operation given to the object prototype.
For array objects, the following example illustrates the purpose of the prototype attribute.
Adds a method to an array object that returns the maximum element value in the array. To do this, declare a function, add it to Array.prototype, and use it.
var data2 = [];
Data2.push(1);
Data2.push(2);
2
After this code executes, y holds the maximum value in the array x, or 6.
3. constructor attribute
Represents the function that creates the object.
object.constructor //object is the name of an object or function.
Note: the constructor attribute is a member of all objects that have prototype. They include all JScript native objects except Global and Math objects. The constructor attribute holds a reference to the function that constructed the particular object instance.
Such as:
x = new String("Hi");
if (x.constructor == String) // To process (the condition is true).
or
var data2 = [];
Data2.push(1);
Data2.push(2);
4
For arrays:
var data2 = [];
Data2.push(1);
Data2.push(2);
5