JS Array Push method Use note

Source: Internet
Author: User
Tags locale shallow copy

JS array of push methods , presumably everyone knows is to add elements to the end of the array, but there is a very important point to note:

Quoted MDN

return value

When the method is called, the new length property value is returned.

var sports = ["Soccer", "baseball"]; var total = Sports.push ("Football", "swimming"//  ["Soccer", "baseball", "Football", " Swimming "]console.log (total);   // 4

The array push returns a length, not a new array, which, if not clear, encounters a large hole in the use process.

Incidentally, remember the method return values for several other arrays:

Pop ()

= [1, 2, 3//  3//  3//  [1, 2]   2
Arr.pop () Returns the value of the element that is removed from the array (returns undefined when the array is empty).

Shift ()

= [1, 2, 3=/ / [2, 3]//  1 Returns the value of the element removed from the array; undefined If the array is empty. Arr.shift ()

Unshift ()

= [1, 2, 3];a.unshift (4, 5); Console.log (a); // [4, 5, 1, 2, 3] arr.unshift (element1, ..., elementn) parameter list element1, ..., elementn the element to be added to the beginning of the array. Returns worth when an object calls the method, its length property value is returned. 
concat ()
The concat () method is used to combine two or more arrays. Instead of changing an existing array, this method returns a new array.  var arr1 = [' A ', ' B ', ' C ']; var arr2 = [' d ', ' e ', ' f ']; var arr3 = arr1.concat (arr2); // ARR3 is a new array ["A", "B", "C", "D", "E", "F"] var new_array = old_array.concat (value1[, value2[, ... [, Valuen]]) The parameter Valuen joins the array and /or values into the new array. returns a value for the new Array instance. 

Splice ()

splice() method to change the contents of an array by deleting existing elements and/or adding new elements.

return value

An array of elements that are deleted. If only one element is deleted, an array containing only one element is returned. If no element is deleted, an empty array is returned.

Slice ()

slice()method returns a start-to-end (not including end) Part of the selected arrayShallow Copyto a new array object, the original array is not modified .

return value:

A new array containing the extracted elements

Summarize:

Both the beginning and the end add are the lengths of the returned array;

Deletions at the beginning and end are returned as deleted elements;

Splice () returns the deleted element;

Concat returns a new array;

Slice returns the extracted array;

 

JS Array Push method Use note

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.