Simple implementation of the increase of the JS array operation

JS provides a number of ways to operate an array, and what is intended to share, delete, change, and check in arrays.

can receive any number of parameters, add them one by one to The array ends and returns the length of the modified number of groups. For example:

VAR Arr = []; var len = arr.push (1); console.log (arr); // [1] console.log (LEN ); // 1LEN = arr.push (2, 3); console.log (arr); // [1, 2, 3] console.log (len); // 3

This method is similar to PUSH (), and can receive any number of parameters, but it is simply added to an array front end. Also return to the new array length. Let’s follow the example:

VAR len = arr.unshift (0); console.log (arr); // [0, 1, 2, 3] Console .log (len); // 4len = arr.unshift (-2, -1); console.log (arr); // [-2, -1, 0, 1, 2, 3] Console.log (LEN ); //6

3, concat ()
 This method is similar to the Push () method, which is also The element is added to the end of the array, but this array is not the original array, but its copy, so the constru will return one after the array.A new array. The specific usage is as follows:   1 Do not pass the parameters, return to the current array copy 
2 to pass one or more arrays, the method will add each of these arrays. In the result array

3 transmits non-array parameters, which will be directly added to the end of the result array

Continue to the top of the chestnut:

VAR Arr1 = Arr.concat (4, [5, 6]); console.log (arr); // [-2, -1, 0, 1, 2, 3] Console. LOG (Arr1); // [-2, -1, 0, 1, 2, 3, 4, 5, 6]

An example in the example, the original array remains No change, 4, 5, 6 elements have been added behind the new array.

4, splice ()

The three methods of the previous method have a large limit, because it is an array before it is not added to an array, and splice () is Not the same, it is very flexible and powerful. Flexible is because it can add an elements to an array of any position, which is powerful because it has the function of deleting and replacing an element in addition to an element (this later will be said).
splice () can add any number of elements to the array specified location, which requires paste at least 3 parameters: start position, 0 (the number of elements to be deleted) and the elements to be added.
 Still then the above example continues:   
Arr.Splice (3, 0, 0.2, 0.4, 0.6, 0.8); Console.log (Arr ); // [-2, -1, 0.8, 1, 2, 3]

As can be seen, splice () PUSH () and unshift () are modified directly on the original number group.

The use of the PUSH () method can be made first out Stack, the method can delete the last item from the end of the array and return to the item.

   VAR item = arr.pop (); console.log (item); // 3console.log (arr); // [-2, -1, 0, 0.2, 0.4, 0.6, 0.8, 1, 2] 
2, shift ()

With the push () method, use can constitute a queue of advanced first, which deletes the first item of the array and returns the item. Continue to follow up:

VAR item = arr.shift (); console.log (item); // -2console.log (Arr ); // [-1, 0.8, 1, 2]

3, Slice ()
 This method is the same as concat (), which does not affect the original array, but SLICE () is used to crop the number of groups, return to cropped arrays, the specific usage is as follows:   The slice () method can accept one or two parameters, that is, the start and end position of the item is returned. In the case of only one parameter, the slice () method returns all items that start from the parameter to the current array. If there are two parameters, the method returns an item between the start and end positions - but does not include an item ended. 
Let’s continue to follow the above example ~~

VAR Arr2 = arr.slice (2, 6); console.log (arr); // [ 1, 0, 0.2, 0.4, 0.6, 0.8, 1, 2] Console.log (Arr2); // [0.2, 0.4, 0.6, 0.8]

4, splice ()
Good, continue to speak this “universal” method.
 Top, the method needs to pass more than 3 parameters when adding array elements, and the second parameter is used to specify the number of points to delete elements, then we pass Number 0. Then, if you only need to delete the element, we just need to pass the two parameters, and the first parameter is used to specify the location of the first item to be deleted, and the second parameter is used to specify whether to delete an element. Number.   Continue to ~~ 
Arr.Splice (2, 4); console.log (arr); // [-1, 0, 1, 2]

4 elements from the position of the index 2 start, so the result is [-1, 0, 1, 2].

Third, change

This actually flexible way is to use splice () this powerful method, in fact, through the above understanding of the method, we are roughly You can know the basic principles of modifying array elements using this method.

The principle is simple, inserting any number of elements into the specified location, and deleting any number of elements at the same time.

Continue in the previous example ~~
 Arr.splice (2, 1, 0.5, 1, 1.5); console.logArr); // [-1, 0, 0.5, 1, 1.5, 2]   
Fourth, Check

IndexOf () and LastIndexOf ()

These two methods receive two parameters: Items to look up and (optional) represents the index of the lookup start position. Where indexof () starts from the beginning (position 0) of the array, the LastIndexOf () method starts looking forward from the end of the array.

VAR index = arr.indexof (0); console.log (index); // 1Index = arr.indexof (3, 0); console.log (index); // -1
 When the element is not found, the -1, a LastIndexOf () method is used.   
The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Clouds.
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment