This article mainly introduces the collection of related information using the array method in JavaScript. For more information, see
Define an array
The Code is as follows:
Var arryMap = {riskId: "<% = riskid %>", riskType: "<% = risktype %> "};
Or
The Code is as follows:
Var arry = [];
Usage:
var risk = arryMap.riskId;Arry.push({id:”1”,name:”1”});Arry.push({id:”2”,name:”2”});
You can also add a single value to the array.
For example:
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
Array usage in js
1. Create an array
Var name = new Array (); // create an Array name [0] = "zhangsan"; // assign name [1] = "lisi" to the Array "; var arrayObj = new Array ([size]); // create an Array and specify the length. Note that it is not the upper limit. It is the length var name = ["zhangsan", "lisi"]. // create an Array and assign the value var name = new Array ("zhangsan", "lisi ");
It should be noted that, although the second method creates an array with a specified length, the array actually gets longer in all cases, that is, even if the length is 5, you can still store elements outside the specified length. Note: The length will change accordingly.
2. Access to array elements
Var testGetArrValue = arrayObj [1]; // obtain the array element value arrayObj [1] = "this is a new value"; // assign a new value to the array element
3. Add array elements
ArrayObj. push ([item1 [item2 [... [itemN]); // Add one or more new elements to the end of the array, and return the new length of the array arrayObj. unshift ([item1 [item2 [... [itemN]); // adds one or more new elements to the array. The elements in the array are automatically removed and the new length of arrayObj is returned. splice (insertPos, 0, [item1 [, item2 [,... [, itemN]); // insert one or more new elements to the specified position of the array. The inserted element is automatically removed and "" is returned "".
4. Deletion of array elements
ArrayObj. pop (); // remove the last element and return the value of arrayObj. shift (); // remove the first element and return the element value. The elements in the array are automatically moved forward to arrayObj. splice (deletePos, deleteCount); // deletes the specified number of deleteCount elements starting from deletePos in the specified position. The removed elements are returned in an array.
5. truncate and merge Arrays
ArrayObj. slice (start, [end]); // return part of the array in the form of an array. Note that the elements corresponding to the end are not included. If the end is omitted, all the elements after the start are copied to arrayObj. concat ([item1 [, item2 [,... [, itemN]); // concatenates multiple arrays (or strings, or arrays and strings) into an array and returns a new connected array.
6. Copy an array
ArrayObj. slice (0); // returns the copy array of the array. Note that it is a new array, not pointing
ArrayObj. concat (); // returns the copy array of the array. Note that it is a new array, not pointing
7. Sorting of array elements
ArrayObj. reverse (); // returns the array address arrayObj. sort (); // sorts array elements and returns the array address.
8. stringized array elements
ArrayObj. join (separator); // returns a string that connects each element value of the array and is separated by separator. ToLocaleString, toString, valueOf: can be considered as a special use of join, not commonly used
2. Three attributes of an array object
1. length attribute
The Length attribute indicates the Length of the array, that is, the number of elements. Because the index of an array always starts from 0, the upper and lower limits of an array are: 0 and length-1. Unlike most other languages, the length attribute of the JavaScript array is variable, which requires special attention. When the length attribute is set to a greater value, the status of the entire array does not actually change, except that the length attribute becomes larger. When the length attribute is set to an hour later than the original value, all the values of the elements whose indexes are greater than or equal to the length in the original array are lost. The following is an example of modifying the length attribute:
Var arr = [,]; // defines an array alert (arr. length); // display the length of the array 10arr. length = 12; // increase the length of the array. alert (arr. length); // The length of the displayed array has changed to 12 alert (arr [8]); // The value of 9th elements is 56arr. length = 5; // reduce the length of the array to 5. The element whose index is equal to or greater than 5 is discarded by alert (arr [8]); // display that 9th elements have changed to "undefined" arr. length = 10; // restore the array length to 10 alert (arr [8]); // although the length is restored to 10, 9th elements cannot be recovered, show "undefined"
From the code above, we can clearly see the nature of the length attribute. However, the length object can be explicitly set and may be implicitly modified. JavaScript can use an undeclared variable, or an undefined array element (an element whose index exceeds or is equal to length, the value of the length attribute is set to the value of the element cited by the column plus 1. For example, the following code:
var arr=[12,23,5,3,25,98,76,54,56,76];alert(arr.length);arr[15]=34;alert(arr.length);
The Code also defines an array containing 10 numbers. The alert statement shows that the length is 10. Then, an element with an index of 15 is used and assigned a value of 15, that is, arr [15] = 34. Then, the array length is output using the alert statement, and the result is 16. In any case, it is a surprising feature for developers who are used to strong-type programming. In fact, the initial length of an Array created in the form of new Array () is 0, and the length of the Array changes only when no element is defined.
From the above introduction, we can see that the length attribute is so magical that it can be used to conveniently increase or decrease the array capacity. Therefore, an in-depth understanding of the length attribute can be used flexibly in the development process.
2. prototype attributes
Returns a reference to an object type prototype. The prototype attribute is common to objects.
ObjectName. prototype
The objectName parameter is the name of the object.
Note: The prototype attribute is used to provide a set of basic functions of the object class. The new instance of the object "inherits" the operation that is granted to the object prototype.
The following example describes the purpose of the prototype attribute for array objects.
Add a method to the array object to return the maximum element value in the array. To do this, declare a function, add it to Array. prototype, and use it.
function array_max( ){var i, max = this[0];for (i = 1; i < this.length; i++){if (max < this[i])max = this[i];}return max;}Array.prototype.max = array_max;var x = new Array(1, 2, 3, 4, 5, 6);var y = x.max( );
After the code is executed, y saves the maximum value in array x, or 6.
3. constructor attributes
The function that creates an object.
Object. constructor // object is the name of an object or function.
Note: The constructor attribute is a member of all objects with prototype. They include all inherent JScript objects except Global and Math objects. The constructor attribute stores references to the functions used to construct a specific object instance.
For example:
X = new String ("Hi"); if (x. constructor = String) // process (the condition is true ).
Or
Function MyFunc {// function body .} Y = new MyFunc; if (y. constructor = MyFunc) // process (the condition is true ).
For arrays:
y = new Array();