


JavaScript study notes (5) Array array type introduction_basic knowledge
Array Creation
First type:
var colors = new Array();
var colors = new Array(20);//Create an array containing 20 items
var colors = new Array("Greg");//Create an array containing 1 item, which is a string Array of "Greg"
var colors = new Array("red","blue","green"); //Create 3 items
Second type:
var colors = ["red","blue","green"] ;
var colors = [];//Create an empty array
Note: The index of the array starts from 0
1. length attribute
length attribute The number of items in the array is saved in, such as:
var colors = ["red","blue","green"];
alert(colors.length); //3
The length attribute is not read-only, you can use the length attribute in the array Remove items at the end, or add new items, such as:
var colors = ["red","blue","green"];
colors.length = 2;
alert(colors); //red,blue
colors[colors.length] = "black";
alert(colors); //red, blue, black
2.join() method, connect the items in the array
var colors = ["red","blue","green"];
alert( colors.join(",")); //red,blue,green
alert(colors.join("||")); //red||blue||green
3. Array stack methods: push() and pop()
push() method can accept any number of parameters, add them to the end of the array one by one, and return the length of the modified array
pop() The method removes the last item from the end of the array, reduces the length value of the array, and returns the removed item
var colors = new Arrary(); //Create an array
var count = colors.push("red","green"); //Push two items to the end of the array
alert(count); //2
count = colors.push("black"); //Push an item to the end of the array
alert(count); //3
var item = colors.pop(); //Remove the last item and return the value
alert(item); //"black"
alert(count); //2
4. Array queue methods: push() and shift(), unshift()
push() method is the same as above
shift() method removes the first item in the array and returns it, and the length of the array is reduced 1 The
unshift() method adds any item to the front of the array and returns the length of the new array
var colors = new Arrary(); //Create an array
var count = colors.push("red","green"); //Push two items to the end of the array
alert(count); //2
count = colors.push("black"); //Push an item to the end of the array
alert(count); //3
var item = colors.shift(); //Remove the first item and return the value
alert(item); //"red"
alert(colors); //green,black
count = colors .unshift("blue"); //Push an item to the front of the array
alert(count); //3
alert(colors); //blue, green, black
5. Reordering methods: reverse() and sort()
reverse() method reverses the order of array items
sort() method defaults to sorting array items in ascending order by string size, and can accept a comparison size The function takes as parameter
var values = [1,2, 3,4,5];
values.reverse();
alert(values); //5,4,3,2,1
//Ascending sorting function
function compare(value1,value2) {
if (value1 < value2) {
return -1; //Descending order is changed to 1
} else if (value1 > value2) {
return 1; //Descending order changed to -1
} else {
return 0;
}
}
//Array sorted in ascending order
var values = [0, 1,5,15,20,10];
values.sort(compare);
alert(values);//0,1,5,10,15,20
//You can use this function for numerical types, ascending order
function compare(value1,value2) {
return value2 - value1;
}
6. Some methods of arrays: concat() method, slice() method and splice() method
The concat() method adds parameters to the end of the original array and returns a new array. The original array remains unchanged.
The slice() method returns the items in the array. If there is one parameter, it returns all items from the specified position to the end of the array. When two parameters are used, the items between the start position and the end position (excluding the end position) are returned, and the original array remains unchanged
The splice() method inserts, deletes, or replaces items in the array, and returns the deleted item (returns an empty array if not deleted), the original array changes
//concat() method
var colors = ["red","green","blue"];
var colors2 = colors.concat("yellow",["black","brown"] );
alert(colors); //red,green,blue
alert(colors2); //red,green,blue,yellow,black,brown
//slice() method
var colors = ["red"," green","blue","yellow","black"];
var colors2 = colors.slice(1); //With one parameter, return all items from the specified position to the end of the array
var colors3 = colors .slice(1,4); //If there are two parameters, return the items between the start position and the end position (excluding the end position)
alert(colors2); //green,blue,yellow,black
alert(colors3); //green,,blue,yellow
//splice() method
//Insert item, specify 3 parameters when inserting: starting position, 0 (item to be deleted), item to be inserted
var colors = ["red","green","blue"];
var inserted = colors.splice(1,0,"yellow","orange"); //Insert two items starting from position 1
alert(colors); //red,yellow,orange,green,blue
alert(inserted); //Empty array
//Replacement item, specify 3 parameters when deleting: starting position, Items to be deleted, any items to be inserted
var colors = ["red","green","blue"];
var replaced = colors.splice(1,1,"black","brown "); //Delete one item and insert two items
alert(colors); //red,black,browm,blue
alert(replaced); //green

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

There are two types of PHP arrays, namely: 1. Index array, the subscript consists of numbers, starting from 0 by default, each number corresponds to the position of an array element in the array; 2. Associative array, the subscript consists of numerical values and characters It is composed of a mixture of strings. If a key name in an array is not a number, then the array is an associative array.

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

Java is a very powerful programming language that is widely used in various development fields. However, during Java programming, developers often encounter ArrayIndexOutOfBoundsException exceptions. So, what are the common causes of this anomaly? ArrayIndexOutOfBoundsException is a common runtime exception in Java. It means that when accessing data, the array subscript exceeds the range of the array. Common reasons include

In PHP programming, array is a frequently used data type. There are also quite a few array operation functions, including the array_change_key_case() function. This function can convert the case of key names in the array to facilitate our data processing. This article will introduce how to use the array_change_key_case() function in PHP. 1. Function syntax and parameters array_change_ke

The data types in PHP arrays are divided into three categories: scalar type, composite type and special type. The eight subcategories are: 1. boolean, Boolean type; 2. integer, integer type; 3. float, floating point type, also known as As double; 4. string, string; 5. array, array; 6. object, object; 7. resource, resource type; 8. NULL, empty null.
