The techniques of adding, deleting, changing and checking _javascript of JavaScript learning notes

Source: Internet
Author: User
Tags array length

The importance of arrays in programming languages is self-evident, and in JavaScript, arrays are one of the most commonly used objects, arrays are ordered collections of values, and because of the weak type, the arrays in JavaScript are flexible and powerful, It's not like a Java. Strong-type advanced language arrays can hold only the same type or its subtype elements, JavaScript may hold multiple types of elements in the same array, and can be dynamically adjusted in length, which can be changed as the data increases or decreases the automatic array length.

An array is a common object in JavaScript, and it has some classic operations, such as adding, deleting, changing, and checking. In this article, the main collation of the relevant methods of operation.

Adding array entries

First let's look at how to add an array to an array. Suppose you have an array:

var arr = [];

An array is declared, but the array is an empty array [], and its length has a value of 0. Next we'll see how to add an array arr to the array. The easiest way to do this is to add an array to an array by index value:

var arr = [];
Arr[0] = ' a ';
Arr[1] = ' B ';
ARR[2] = 1;
ARR[3] = 2;
Console.log (arr); ["A", "B", 1, 2]
console.log (arr.length);/4

Alternatively, you can add an array item to an array by changing the length value of the array, but this method adds an array of undefined to the array:

var arr = [];
Arr[0] = ' a '; Add an ' a ' array to the array arr
arr.length = 5;//Change the ' length ' value of the array to ' 5 '
console.log (arr);//["A", undefinedx4]

Although this method also adds an array of items to an array, it is relatively cumbersome. Actually adding array items to an array is not so cumbersome, you can add an array item to an array by using the native method provided by the array.

Push ()

Using the push () method, you can add one or more array items to the end of a list.

var arr = [];
Arr.push (' A ', ' B ');
Console.log (arr); [' A ', ' B ']
unshift ()

With the push () method, you can add one or more array items to the end of the arrays, then use the Unshift () method to add one or more array items to the front of the list:

var arr = [' A ', ' B '];
Arr.unshift (1,2);
Console.log (arr); [1, 2, "a", "B"]

In addition to these two methods, you can also use the splice () method to add an array item to an array:

var arr = [' A ', ' B ', ' C ', 1,2];
Arr.splice (2,0, ' d ', ' C ', ' e ');
Console.log (arr); ["A", "B", "D", "C", "E", "C", 1, 2]

In addition to the splice () method, you can use the Concat () method to add an array item to an array, but using this method does not alter the original array, creating a new array in the original array:

var arr = [' A ', ' B ', ' C '];
var arr2 = arr.concat (' d ', 1,2,[' E ', 3]);
Console.log (arr); ["A", "B", "C"]
Console.log (ARR2);//["A", "B", "C", "D", 1, 2, "E", 3]

Delete an array item

For an array operation, in addition to adding an array of items, many times it is necessary to delete operations. Common methods for removing array items are pop () and shift ().

Pop ()

The Pop () method deletes an array item from the end of the array:

var arr = [' A ', ' B ', ' C ', ' d ', 1,2];
Arr.pop ();
Console.log (arr); ["A", "B", "C", "D", 1]

Shift ()

The shift () method is just the opposite of the pop () method, which deletes the first item of the array:

var arr = [' A ', ' B ', ' C ', ' d ', 1,2];
Arr.shift ();
Console.log (arr); ["B", "C", "D", 1, 2]

Whether the pop () or the shift () method can only delete an array item at a time, it is relatively cumbersome to delete an array item. In the operation of an array, in addition to the two methods, you can delete an array item through the slice () and splice () methods.

Slice ()

The slice () method can remove multiple array entries from an array, except that slice () does not affect the original array, but creates an array copy on the base of the original array:

var arr = [1,2,3,4, ' A ', ' B '];
var arr2 = Arr.slice (2);
Console.log (arr); [1, 2, 3, 4, "A", "B"]
Console.log (ARR2);//[3, 4, "A", "B"]
Console.log (ARR3);//["A", "B"]

Splice ()

In addition to adding array items to an array, the splice () method can also remove an array entry for an array:

var arr = [1,2,3,4, ' A ', ' B ', ' C '];
var arr2 = Arr.splice (2,2);
Console.log (arr); [1, 2, "a", "B", "C"]
Console.log (ARR2);//[3, 4]

Change array

The splice () method in an array is a powerful method in an array that, in addition to adding an array item to an array and deleting an array item, can also change an array:

var arr = [1,2,3,4,5,6];
var arr2 = Arr.splice (2,3, ' a ', ' B ', ' C ');
Console.log (arr); [1, 2, "a", "B", "C", 6]
console.log (ARR2);//[3, 4, 5]

Arrays of Queries

The array query mentioned here is actually the query extraction of the array. The method used is the slice () method:

var arr = [1,2,3,4,5,6];
var arr2 = Arr.slice ( -3);
Console.log (arr); [1, 2, 3, 4, 5, 6]
console.log (ARR2);//[4, 5, 6]

Summarize

Here a simple collation of an array of increase, delete, change, check the relevant methods. A simple summary:

Adding an array Item method: In addition to directly changing the values of the array items and modifying the length of the array to add an array Item method to the array, you can also use push (), Unshift (), concat (), and Splice () to add an array item

To delete an array Item method: Delete An array Item method with POPs (), shift (), slice (), and Splice () methods

To change the array Item method: The array item is changed primarily by the splice () method in the array

Query Array Item Method: Query Array method is actually a query extraction function of the log group, the main use of the method is slice () method

You can click here for the pop (), push (), shift (), and Unshift () methods, and the introduction to the concat (), slice (), and Splice () method can be clicked here.

About JavaScript Learning notes of the array of increase, delete, change, check small series to introduce here, I hope to help you! For more information about JavaScript, please visit the website of Yun-Qi community for more information!

Related Article

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.