Js array operations are commonly used

  • 2020-03-30 02:53:32
  • OfStack

There is a lot of traversal in the case of JSON arrays in jquery, but it doesn't seem like much to remove them with additions.

Today, I tried json[I].remove(),json.remove(I) after all, look at the web page of the DOM object seems to json data in the form of an array, look up the next related JS array operation is really cool.

Write it down.

1. Creation of array


var arrayObj = new Array(); //Create an array
var arrayObj = new Array([size]); //Create an array And specify the length, not the upper bound, the length 
var arrayObj = new Array([element0[, element1[, ...[, elementN]]]]); //Create an array And the assignment 

Note that while the second method of creating an array specifies a length, in all cases the array actually gets longer, which means that even if you specify a length of 5, you can still store the element outside the specified length. Note that the length changes.

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. Adding array elements


arrayObj. push([item1 [item2 [. . . [itemN ]]]]);//Adds one or more new elements to the end of the array and returns the new length of the array
arrayObj.unshift([item1 [item2 [. . . [itemN ]]]]);//When one or more new elements are added to the beginning of the array, the elements in the array automatically move back, returning the new length of the array
arrayObj.splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);//Inserts one or more new elements into the specified position in the array. The inserted element automatically moves back, returning "".

4. Delete array elements


arrayObj.pop(); //Removes the last element and returns the value of that element
arrayObj.shift(); //Removes the previous element and returns the value of that element. Elements in the array move forward automatically
arrayObj.splice(deletePos,deleteCount); //Deletes the specified number of deleteCount elements starting from the specified location deletePos, and returns the removed elements as an array

5. Intercept and merge arrays


arrayObj.slice(start, [end]); //Returns a portion of the array in the form of an array. Note that the element corresponding to the end is not included
arrayObj.concat([item1[, item2[, . . . [,itemN]]]]); //Concatenate multiple arrays (which can be strings, or a mix of arrays and strings) into one array, returning a new concatenated array

6. Copy of the array


arrayObj.slice(0); //Returns a copy of the array. Note that it is a new array, not a pointer
arrayObj.concat(); //Returns a copy of the array. Note that it is a new array, not a pointer

7. Sorting array elements


arrayObj.reverse(); //Invert the element (first to last, last to first) to return the array address
arrayObj.sort(); //Sort the array elements and return the array address

8. Stringing of array elements


arrayObj.join(separator); //Returns a string that concatenates the values of each element of the array, separated by separator.

ToLocaleString, toString, valueOf: can be thought of as a special use of join, not commonly used


Related articles: