Home > Article > Backend Development > Operation of json array in jquery: addition, deletion and modification
Add, delete, and modify json array operations in jquery
1. Array creation
var arrayObj = new Array(); //Create an array
var arrayObj = new Array([size]); //Create an array and specify The length, please note that it is not the upper limit, but the length
var arrayObj = new Array([element0[, element1[, ...[, elementN]]]]); Create an array and assign the value
It should be noted that although the second This method creates an array and specifies the length, but in fact the array is variable-length in all cases. That is to say, even if the length is specified to be 5, elements can still be stored outside the specified length. Note: At this time, the length will change accordingly. Change.
2. Access to array elements
var testGetArrValue=arrayObj[1]; //Get the element value of the array
arrayObj[1]= "This is a new value"; //Assign a new value to the array element
3. Adding 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 [... splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);//Insert one or more new elements into the specified position of the array, and the element at the insertion position will be automatically moved back, return "".
4. Deletion of array elements
arrayObj.pop(); //Remove the last element and return the element value
arrayObj.shift(); //Remove the first element and return the element value, array The elements in the middle are automatically moved forward
arrayObj.splice(deletePos,deleteCount); //Delete the specified number of deleteCount elements starting from the specified position deletePos, and return the removed elements in array form
5. Interception and merging of arrays
arrayObj.slice(start, [end]); //Return a part of the array in the form of an array. Note that the element corresponding to end is not included. If end is omitted, all elements after start will be copied
arrayObj.concat([item1[, item2[, . . . [,itemN]]]]); //Concatenate multiple arrays (can also be strings, or a mixture of arrays and strings) into one array, and return the connected new array
6. Copy of array
arrayObj.slice(0); //Returns a copy array of the array, note that it is a new array, not pointing to
arrayObj.concat(); //Returns a copy array of the array, note that it is A new array, not pointing to
7. Sorting of array elements
arrayObj.reverse(); //Reverse the elements (the first to the last, the last to the front), return the array address
arrayObj.sort (); // Sort the array elements and return the array address
8. Stringification of the array elements
arrayObj.join(separator); // Return a string. This string connects each element value of the array in together, separated by a separator.
toLocaleString, toString, valueOf: can be regarded as special usage of join, not commonly used