Add, delete, modify, and query the array of JavaScript learning notes-javascript tutorial

Source: Internet
Author: User
This article describes how to add, delete, modify, and query arrays in JavaScript learning notes. For more information, see the importance of arrays in programming languages, arrays in JavaScript are also one of the most commonly used objects. arrays are ordered sets of values. Due to the weak type, arrays in JavaScript are flexible and powerful, unlike arrays of high-level languages such as Java, JavaScript can store multiple types of elements in the same array, and the length can be dynamically adjusted, the array length can be automatically changed as data increases or decreases.

An array is a common object in JavaScript. It has some classic operations, such as adding, deleting, modifying, and querying arrays. In this article, we will mainly sort out relevant operations in this regard.

Add array items

First, let's look at how to add an array to an array. Suppose there is an array:

var arr = [];

An array is declared above, but this array is an empty array [], and its length value is 0. Next, let's look at how to add array items to array arr. The simplest method is to add an array item to the array by indexing the 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

In addition, you can add an array item to the array by changing the length value of the array. However, the array items added to the array by this method are all undefined:

Var arr = []; arr [0] = 'a'; // Add an 'A' array item arr to the array arr. length = 5; // change the 'length' value of the array to '5' console. log (arr); // ["a", undefined × 4]

Although this method also adds array items to the array, it is relatively troublesome. In fact, adding an array item to an array is not so troublesome. You can use the native method provided by the array to add an array item to the array.

Push ()

You can use the push () method to add one or more array items to the end of the array.

var arr = [];arr.push('a','b');console.log(arr); // ['a','b']unshift()

The push () method can be used to add one or more array items to the end of the array. Then, the unshift () method can be used to add one or more array items before the array:

var arr = ['a','b'];arr.unshift(1,2);console.log(arr); // [1, 2, "a", "b"]

In addition to the two methods, you can also use the splice () method to add array items to the 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 also use the concat () method to add array items to the array, but this method will not change the original array, and a new array will be created 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 array items

In addition to adding array items, you also need to delete arrays. You can use pop () or shift () to delete an array.

Pop ()

The pop () method deletes an array entry 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 the opposite of the pop () method. It can delete the first entry of the array:

var arr = ['a','b','c','d',1,2];arr.shift();console.log(arr); // ["b", "c", "d", 1, 2]

The pop () or shift () method can only delete one array item at a time for the array, but it is relatively troublesome to delete the array item in most cases. In addition to these two methods, you can also delete array items through the slice () and splice () methods.

Slice ()

The slice () method can delete multiple array items from an array, but the difference is that slice () does not affect the original array, but creates an array copy based on 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 the array, the splice () method can also delete array items from the 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 the array is a powerful method in the array. In addition to adding an array item to the array and deleting an array item, you 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]

Array Query

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

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]

Summary

The following describes how to add, delete, modify, and query an array. A brief summary:

Add an array item method: In addition to directly changing the value of the array item and modifying the length of the array to add an array item method, you can also use push (), unshift (), concat () and splice () to add array items

Methods for deleting array items: the methods for deleting array items include pop (), shift (), slice (), and splice ().

Method for changing array items: in the array, the splice () method is used to change the array items.

Method for querying array items: the method for querying array items is actually to query and extract arrays. The main method used is the slice () method.

You can click here for pop (), push (), shift (), and unshift () operations. For more information, see concat (), slice (), and splice () for details about the method, click here.

We will introduce the addition, deletion, modification, and query of the array of JavaScript learning notes here, and hope to help you! For more information about javascript, visit the official website of the script home for details!

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.