Push () in jquery (adding elements to arrays) _ jquery

Source: Internet
Author: User
This article describes how to use jquery push. You can add one or more elements to the end of the array and return a new length. For more information, see Definition and usage of push

The push () method can add one or more elements to the end of an array and return a new length.

Syntax

ArrayObject. push (newelement1, newelement2,..., newelementX)

Parameter description
Newelement1 is required. The first element to be added to the array.
Newelement2 is optional. The second element to be added to the array.
NewelementX is optional. You can add multiple elements.


Return Value

Adds the specified value to the new length after the array.

Description

The push () method can add its Parameter order to the end of arrayObject. It directly modifies the arrayObject instead of creating a new array. The push () and pop () methods use the advanced post-stack functions provided by arrays.

Tips and comments

Note: This method will change the length of the array.
Tip: to add one or more elements at the beginning of an array, use the unshift () method.

Instance

In this example, we will create an array and add an element to change its length:

The Code is as follows:


Var arr = new Array (3 );
Arr [0] = "George ";
Arr [1] = "John ";
Arr [2] = "Thomas ";
Document. write (arr + "") document. write (arr. push ("James") + "") document. write (arr );

Output:
George, John, Thomas
4
George, John, Thomas, James

One usage on the Internet:

The Code is as follows:


$ (Function (){
Var buf = [];
Buf. push ('

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.