Common Operations on arrays in JavaScript programs

Source: Internet
Author: User
Introduction: Array. Array in JavaScript is a common type in JavaScript, and arrays in JavaScript are quite different from those in other languages. The data types stored in arrays in JavaScript are not necessarily the same, and the length of arrays can also be changed. Below is a record of common operations on the Array: Create a new Array
Introduction to the article: array in JavaScript.

Array is a common type in JavaScript, and arrays in JavaScript differ greatly from arrays in other languages. The data types stored in arrays in JavaScript are not necessarily the same, and the length of arrays can also be changed.

The following is a record of common operations on the Array:

1. Create an array

You can use the following method to create an array:

Var obj = ['zdy ', 22, 'hubei Wuhan', 'computer Science and Technology ']; // create an array variable
Var obj1 = new Array ('zdy ', 22, 'hubei Wuhan', 'computer Science and Technology ');
Var obj2 = new Array (4 );
Obj2 [0] = 'zdy ';
Obj2 [1] = 22;
Obj2 [2] = 'hubei Wuhan ';
Obj2 [3] = 'computer Science and Technology ';
The above method is equivalent to creating an array. The output is as follows:
Alert (obj); // return zdy, 22, Wuhan, Hubei province, computer science and technology
Alert (obj1); // return zdy, 22, Wuhan, Hubei province, computer science and technology
Alert (obj2); // return zdy, 22, Wuhan, Hubei province, computer science and technology

The data types stored in arrays in the JavaScript mentioned above are not necessarily the same. Therefore, we can also create the following Arrays:

Var obj3 = [
'Zdy ',
[1, '2', true],
{
Name: 'zdy ',
Age: 22,
Address: 'hubei Wuhan'
}
];
Let's take a look at the output:
Alert (obj3); // returns zdy, 1, 2, true, [object Object]
Alert (obj3 [1] [2]); // return true
Alert (obj3 [2]. address); // return to Wuhan, Hubei Province
Introduction to the article: array in JavaScript.
Common Methods in Arrays 1. toString ()/toLocaleString ()/valueOf () method

Objects or arrays all have these three methods. These three return values are the same. They are concatenated into strings and returned. They are separated by commas by default. For example:

Var obj = ['zdy ', 22, 'hubei Wuhan', 'computer Science and Technology ']; // create an array variable
Alert (obj. toString (); // toString () method, returns zdy, 22, Wuhan, Hubei, and computer science and technology
Alert (obj. toLocaleString (); // toLocaleString () method, returns zdy, 22, Wuhan, Hubei, computer science and technology
Alert (obj. valueOf (); // valueOf () method, return zdy, 22, Wuhan, Hubei, computer science and technology
2. join () method

The strings returned by the preceding three methods are separated by commas (,). If we need to replace them with other delimiters, we need to use the join method. This method Concatenates the characters according to the specified characters and returns them.

Var obj = ['zdy ', 22, 'hubei Wuhan', 'computer Science and Technology '];
Alert (obj. join ("|"); // returns zdy | 22 | Wuhan, Hubei Province | Computer Science and Technology
3. Stack method-push ()/pop ()

JavaScript provides the push ()/pop () method, so that we can operate the array like the stack (Advanced and later). The insertion and deletion of the array are at the top of the stack (the end of the array).

The pusp () method can accept any number of parameters to add these parameters to the end of the array and return the length of the added array.

Pop () deletes the last element of the array and returns the deleted element.

Var obj = ['zdy ', 22, 'hubei Wuhan', 'computer Science and Technology ']; // create an array variable
Alert (obj. push ("computer", 160); // append an element to the end of the array. 6 is returned.
Alert (obj); // return zdy, 22, Wuhan, Hubei province, computer science and technology, computer, 160
Alert (obj. pop (); // Delete the last element of the array and return the deleted element 160.
Alert (obj); // return zdy, 22, Wuhan, Hubei province, computer science and technology, computer
4. Queue method-push ()/shift ()/unshift ()

JavaScript also provides the shift ()/unshift () method, so that we can operate arrays like Queue (first-in-first-out) operations, the insertion and deletion of arrays are performed at the end (at the end of the array) and at the right (at the beginning of the array.

The pusp () method can accept any number of parameters to add these parameters to the end of the array and return the length of the added array.

The shift () method deletes an element from the front end of the array and returns the deleted element.

The unshift () method inserts an element from the front end of the array and returns the length of the new array.

Var obj = ['zdy ', 22, 'hubei Wuhan', 'computer Science and Technology ']; // create an array variable
Alert (obj. push ("added element"); // append an element to the end of the array and return 5
Alert (obj); // return zdy, 22, Wuhan, Hubei province, computer science and technology, elements added
Alert (obj. shift (); // Delete the first element in the array and return the deleted element zdy.
Alert (obj); // return 22, Wuhan, Hubei province, computer science and technology, added elements
Alert (obj. unshift ("add Header element"); // Add a new element to the array header, and return the array length of 5
// ******** Note: in IE, unshift returns undefined **********
Alert (obj); // returns the newly added Header element, 22, Wuhan, Hubei province, computer science and technology, and the added element.
Introduction to the article: array in JavaScript.
Basic operations on Arrays 1. concat () method

The concat () method can be used to create an array based on an existing number of groups to connect to the array.

Var a1 = [1, 2, 3];
Var a2 = a1.concat (); // create a new array, add two elements to a1, and assign them to a2.
Alert (a1); // returns 1, 2, 3
Alert (a2); // returns 1, 2, 3, 4, 5
2. slice () method

The slice () method is used to extract some elements from the array.

Var a1 = [1, 2, 3, 4, 5, 6];
Var a2 = a1.slice (2nd); // returns elements between 4th and 4th, excluding
Alert (a1); // returns 1, 2, 3, 4, 5, 6
Alert (a2); // returns 3, 4
3. splice () method

The splice () method is used to insert, modify, and delete elements into an array.

// Insert in splice
Var obj = ['zdy ', 22, 'hubei Wuhan', 'computer Science and Technology ']; // create an array variable
Obj. splice (123, "newly inserted element 1",); // insert two elements from the first one
Alert (obj); // return zdy, newly inserted element 1 ", 123,22, Wuhan, Hubei, computer science and technology
// Modify in splice
Var obj = ['zdy ', 22, 'hubei Wuhan', 'computer Science and Technology ']; // create an array variable
Alert (obj); // return zdy, 22, Wuhan, Hubei province, computer science and technology
Alert (obj. splice (, 19); // modify the first element to 19 and return the modified element 22.
Alert (obj); // return zdy, 19, Wuhan, Hubei province, computer science and technology
Alert (obj. splice (, 20, "Hubei Enshi "));
// Modify the two elements starting from the first element to 20 and "Hubei Enshi"
// Return 19, Wuhan, Hubei Province
Alert (obj); // returns zdy, 20, Enshi, Hubei Province, computer science and technology
// Delete In splice
Var obj = ['zdy ', 22, 'hubei Wuhan', 'computer Science and Technology ']; // create an array variable
Alert (obj); // return zdy, 22, Wuhan, Hubei province, computer science and technology
Alert (obj. splice (); // deletes two elements starting with the first element, and returns the deleted element 22, Wuhan, Hubei Province.
Alert (obj); // returns zdy, computer science and technology

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.