ES6 Learning Notes (2) arrays (middle)

Source: Internet
Author: User

Next, let's share some other methods of the array. You can also click here to learn more about the array method

Concat Method:

Overview: The concat () method merges an incoming array or non-array value with the original array, forming a new array and returning it.

Parameters:

ValueN: An array or non-array value that needs to be merged with the original array.

Describe:

The Concat method creates a new array, then places the elements in the object that invokes it (this object) and the elements in the parameters of the array type in all parameters, as well as the parameters of the non-array type, into the new array in order, and returns the array.

The concat method does not modify the values of the objects that call it (the object to which it is this directed) and the individual arrays in the parameters, but instead copies each of their elements into a new array of combinations. The elements in the original array are copied in two ways:

    • Object reference (Non-object direct volume): The concat method copies the object reference into a new array of combinations, the original array and the object references in the new array point to the same actual object, so when the actual object is modified, two arrays are also modified.
    • String and number (is the original value, not the string and number object that wraps the original value ): The concat method copies the values of the strings and numbers into the new array.

Example:

      varNUM1 = [1, 2, 3]; varnum2 = [4, 5, 6]; varNUM3 = [7, 8, 9]; Console.log (Num1.concat (num2, num3));//make up a new array [1, 2, 3, 4, 5, 6, 7, 8, 9]; original array num1, NUM2, num3 unmodifiedConsole.log (NUM1);//[1, 2, 3]Console.log (NUM2);//[4, 5, 6]Console.log (NUM3);//[7, 8, 9]Console.log (Num1.concat (NULL, [1, ' A ', ' B ',, ' f ']) + ' concat method ');//1,2,3,,1,a,b,,fconcat Method

With the above code, you will find that concat does not change the array that calls the method and is called by an index that does not have a value .

Splice () Method:

Overview: method adds/Deletes an item to/from an array, and then returns the item that was deleted. splice()

Parameters:

start?:from which one of the array to start modifying the content. If the length of the array is exceeded, the content is added starting at the end of the array, or , in the case of a negative value, the first from the bottom of the array.

An deleteCount: integer that represents the number of array elements to remove. If deleteCount It is 0, the element is not removed. In this case, you should add at least one new element. If deleteCount it is greater than the start total number of elements that follow, then the elements from the start subsequent will be deleted (with the start bits).

    itemN:The element to add into the array. If not specified, splice() only the array element is deleted.

Describe:

If the number of elements added into an array is not equal to the number of elements being deleted, the length of the array will change accordingly.

Example:

Example 1:

      varNUM1 = [1, 2, 3]; varnum2 = [4, 5, 6]; varNUM3 = [7,,9,10,11]; Console.log (Num1.splice (1, 0, ' 4 '));//[]Console.log (NUM1);//[1, "4", 2, 3]Console.log (Num2.splice (), ' Y ');//[5]Console.log (NUM2);//[4, "Y", 6]Console.log (Num3.splice (1,0,8));//[]Console.log (NUM3);//[7, 8, 3:9, 4:10, 5:11]

Example 2:

      varNUM1 = [1, 2, 3]; varnum2 = [4, 5, 6]; varNUM3 = [7,,9,10,11]; Console.log (Num1.splice (-1,0, ' 4 '));//[]Console.log (NUM1);//[1, 2, "4", 3]Console.log (Num2.splice ( -1,1, ' y '));//[5]Console.log (NUM2);//[4, "Y", 6]Console.log (Num3.splice ( -1,0,8));//[]Console.log (NUM3);//[7, 8, 3:9, 4:10, 5:11]
Unshift Method:

Overview: the Unshift () method adds one or more elements at the beginning of the array and returns the array's new length value.

Parameters:

Element1, ..., elementn: the element to add to the beginning of the array.

Describe:

unshiftThe Array-like method inserts the given argument at the beginning of the class array (*. *) object where it is called.

Example:

      var num1 = [1, 2, 3];       var num2 = [4, 5, 6];      Console.log (Num1.unshift (1, ' 4 ')); // 5      Console.log (NUM1); // [1, "4", 1, 2, 3]      Console.log (Num2.unshift (1, 1, ' 4 ')); // 6      Console.log (NUM2); // [1, 1, "4", 4, 5, 6]
Copywithin Method:

Overview: copyWithin() The method will simply copy some elements of the array to different locations in the same array, without changing the size of the array, returning the array.

Parameters:

target:0 is the index of the base, copying the sequence to that location. If it is a negative number, it is target calculated from the end. If target it is greater than arr.length or equal, no copy will occur. If target start After, the copied sequence is modified to conform arr.length .

    start:0 is the base index at which to begin copying the starting position of the element. If it is a negative number, it is start calculated from the end. If start it is ignored, it copyWithin will be copied starting from 0.

end:0 is the base index at which to begin copying the end position of the element. copyWithinwill be copied to that location, but not the end elements of this location. if it is a negative number, it is end calculated from the end. If the end 被忽略, Copywithin will be copied to the arr.length .

Describe:

The arguments Target,start and end must be integers.

If start is negative, its specified index position is equal to the length of the length+start,length array. The same is true for end.

The Copywithin method does not require its this value to be an array object; In addition, Copywithin is a mutable method that can change the this object itself and return it, not just its copy.

Copywithin are like C and C + + memcpy 函数一样,且它是用来移动  Array or TypedArray 数据的一个高性能的方法。复制以及粘贴序列这两者是为一体的操作;即使复制和粘贴区域重叠,粘贴的序列也会有拷贝来的值。

the copywithin function is designed to be generic and does not require its this value to be an 数组 object.

The Copywithin is a mutable method that does not change the length of this, but changes the contents of this itself and creates new properties when needed.

Example:

Console.log ([' A ', ' B ', ' d ', ' F '].copywithin (-1));//["A", "B", "D", "a"]Console.log ([' A ', ' B ', ' d ', ' F '].copywithin (-2));//["A", "B", "A", "B"]Console.log ([' A ', ' B ', ' d ', ' F '].copywithin (-3));//["A", "a", "B", "D"]Console.log (' two parameters ')); Console.log ([' A ', ' B ', ' d ', ' f ', ' G '].copywithin (0,-1));//["G", "B", "D", "F", "G"]Console.log ([' A ', ' B ', ' d ', ' f ', ' G '].copywithin (0,-2,-1));//["F", "B", "D", "F", "G"]Console.log ([' A ', ' B ', ' d ', ' f ', ' G '].copywithin (0,-3,-1));//["D", "F", "D", "F", "G"]Console.log ([' A ', ' B ', ' d ', ' f ', ' g ', ' E '].copywithin (0,-5,-1));//["B", "D", "F", "G", "G", "E"]      //The value of the array is =copy-1 when the array is larger than the length of the array, and the original data is deleted in the middle of the redundant      //for example [' A ', ' B ', ' d ', ' f ', ' g ', ' E '].copywithin (0,-5,-1) The execution should have been ( -5,-1): B,d,f,g,e, the original number: B,d,f,g,e      //because the length of the original array is exceeded, deleting part of B,d,f,g,e deletes the E in the B,d,f,g, B,d,f,g,e, and then deletes the b,d,f,      //so the end result is: B,d,f,g,g,eConsole.log (' parameter is a positive integer '); Console.log ([' A ', ' B ', ' d ', ' f ', ' g ', ' E '].copywithin (1,1,3));//["A", "B", "D", "F", "G", "E"]Console.log ([' A ', ' B ', ' d ', ' f ', ' g ', ' E '].copywithin (2,1,5));//["A", "B", "B", "D", "F", "G"]Console.log ([' A ', ' B ', ' d ', ' f ', ' g ', ' E '].copywithin (2,1,5));//["A", "B", "B", "D", "F", "G"]Console.log (' haha ha 222 ')); Console.log ([' A ', ' B ', ' d ', ' F '].copywithin (1,-1,-3));//["A", "B", "D", "F"]
Pop method:

Overview : method pop() deletes the last element in an array and returns the element.

Describe:

popmethod deletes the last element in an array and returns the deleted element to the caller.

popis intentionally designed to be universal, the method can be applied through call or apply to a class array (Array-like) object.

Example:

      var num1 = [1, 2, 3];      Console.log (Num1.pop ()); // 3      Console.log (NUM1); // [1, 2]
Shift Method:

Overview: shift() method deletes the first element of the array and returns the element. The method changes the length of the array.

Describe:

shiftMethod removes the element with an index of 0 (that is, the first element) and returns the removed element, and the index value of the other element is reduced by 1. If length the value of the property is 0 (length is 0), it is returned undefined .

shiftThe method is not limited to arrays: The method can also pass call or apply Act on the object. For objects that do not contain the length property, a length property with a value of 0 is added.

Example:

      var num1 = [1, 2, 3];      Console.log (Num1.shift ()); // 1      Console.log (NUM1); // [2, 3]
Push method:

Overview: The push() Add method adds one or more elements to the end of the array and returns the new length of the array (the Length property value).

Describe:

elementN:The element that is added to the end of the array.

Example:

Example 1:

 var num1 = [1, 2, 3var num2 = [4, 5, 6var num3 = [7, 8, 9,,11//4 console.log (num1+ ' push method '); ////[4, 5, 6] Console.log (Num2.push (num3)); //4 console.log (num2+ ' push method ');  4,5,6,7,8,9,,11push method Console.log (NUM3); //[7, 8, 9, 4:11]       

Example 2:

      var num1 = [1, 2, 3];       var num2 = [4, 5, 6];       var num3 = [7, 8, 9,,11];      Console.log (Num3.push (num2)); // 6      Console.log (num3+ ' push method '); // 7,8,9,,11,4,5,6push Method

ES6 Learning Notes (2) arrays (middle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.