SoFunction
Updated on 2025-04-06

js array operation code collection

I have been using js for a long time, but I have never delved into the array form of js. Occasionally, it is simple (char). A project I have done during this period uses a lot of arrays. I think I am a master of JS and I have no idea how to start. I am cruel and I will learn it! hehe. After learning, I realized that the function of js array is very powerful, much stronger than VB and C#. Let's take a look at it slowly.

1. Creation of arrays

Copy the codeThe code is as follows:

var arrayObj = new Array(); //Create an array
var arrayObj = new Array([size]); //Create an array and specify the length, note that it is not the upper limit, it is the length
var arrayObj = new Array([element0[, element1[, ...[, elementN]]]]); //Create an array and assign a value

It should be noted that although the second method creates an array that specifies the length, in fact, the array is longer in all cases. That is to say, even if the length is specified, the element can still be stored outside the specified length. Note: the length will change accordingly.

2. Access to elements of array

Copy the codeThe code is as follows:

var testGetArrValue=arrayObj[1]; //Get the element value of the array
arrayObj[1]= "This is a new value"; // Assign a new value to the array element


3. Adding array elements

Copy the codeThe code is as follows:

arrayObj. push([item1 [item2 [. . . [itemN ]]]);// Add one or more new elements to the end of the array and return the new length of the array
([item1 [item2 [. . . [itemN ]]]);// Add one or more new elements to the array to start, the elements in the array will automatically move backwards, and return the new length of the array
(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);//Insert one or more new elements into the specified position of the array, the elements at the insertion position will automatically move backwards, and return "".


4. Deletion of array elements

Copy the codeThe code is as follows:

(); //Remove the last element and return the value of the element
(); //Remove the last element and return the value of the element, the elements in the array will automatically move forward
(deletePos,deleteCount); //Delete the element of the specified number of deleteCount starting from the specified position deletePos, and returns the removed element in the array form


5. Intercept and merge of arrays

Copy the codeThe code is as follows:

(start, [end]); //Return part of the array as an array, note that the elements corresponding to end are not included. If end is omitted, all elements after start will be copied
([item1[, item2[, . . . [,itemN]]]]); //Connect multiple arrays (can also be strings, or a mixture of arrays and strings) into an array, and return the connected new array


6. Copy of array

Copy the codeThe code is as follows:

(0); //Return the copy array of the array, note that it is a new array, not pointing to
(); //Return the copy array of the array, note that it is a new array, not pointing to


7. Sort array elements

Copy the codeThe code is as follows:

(); //Reverse the element (the first one is ranked last, the last one is ranked last), return the array address
(); //Sort the array elements and return the array address


8. Stringing of array elements

Copy the codeThe code is as follows:

(separator); //Returns a string, which connects each element value of the array together, separated by a separator.
toLocaleString , toString , valueOf: It can be regarded as a special usage of join, not often used


2. Three properties of array object

1. length attribute

The Length attribute represents the length of the array, that is, the number of elements in it. Because the index of an array always starts from 0, the upper and lower limits of an array are: 0 and length-1 respectively. Unlike most other languages, the length property of JavaScript arrays is mutable, which requires special attention. When the length attribute is set to be larger, the state of the entire array will not actually change, just the length attribute becomes larger; when the length attribute is set to be smaller than the original, all the values ​​of elements with indexes greater than or equal to length in the original array are lost. Here is an example demonstrating changing the length attribute:
Copy the codeThe code is as follows:

var arr=[12,23,5,3,25,98,76,54,56,76];
//Define an array containing 10 numbers
alert(); //Show the length of the array is 10
=12; //Increase the length of the array
alert(); //Show the length of the array has changed to 12
alert(arr[8]); //Show the value of the 9th element, 56
=5; //Reduce the length of the array to 5, and elements with index equal to or exceeding 5 will be discarded
alert(arr[8]); //Show the 9th element has become "undefined"
=10; //Restore the array length to 10
alert(arr[8]); //Although the length is restored to 10, the 9th element cannot be retracted, showing "undefined"


From the above code we can clearly see the properties of the length attribute. But the length object can not only be explicitly set, it may also be implicitly modified. An undeclared variable can be used in JavaScript. Similarly, an undefined array element (referring to an element whose index exceeds or is equal to length). At this time, the value of the length attribute will be set to the value of the used element index plus 1. For example, the following code:

Copy the codeThe code is as follows:

var arr=[12,23,5,3,25,98,76,54,56,76];
alert();
arr[15]=34;
alert();


The code also first defines an array containing 10 numbers. Through the alert statement, it can be seen that its length is 10. Then, an element with an index of 15 is used, and the value is 15, that is, arr[15]=34. At this time, the length of the array is output using the alert statement, and the result is 16. Anyway, this is a surprising feature for developers accustomed to strongly typed programming. In fact, an array created using the form of new Array() has an initial length of 0. It is the operation of undefined elements that changes the length of the array.

From the above introduction, we can see that the length attribute is so magical that it can easily increase or decrease the capacity of the array. Therefore, an in-depth understanding of the length attribute will help to flexibly apply it during the development process.

2. Prototype attributes

Returns a reference to the object type prototype. The prototype attribute is shared by object.

The objectName parameter is the name of the object object.
Description: Use the prototype attribute to provide a set of basic functions of an object's class. A new instance of an object "inherits" the operation that gives the object prototype.

For array objects, use the following example to illustrate the purpose of the prototype attribute.

Add a method to the array object to return the maximum element value in the array. To accomplish this, declare a function, join it, and use it.

Copy the codeThe code is as follows:

function array_max()
{
var i,
max = this[0];
for (i = 1; i < ; i++)
{
if (max < this[i])
max = this[i];

}
return max;

}
= array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = ();


After this code is executed, y saves the maximum value in the array x, or 6.

3. constructor attribute

A function that represents the creation of an object.
//object is the name of an object or function.
Description: The constructor property is a member of all objects with prototype. They include all JScript-inherited objects except Global and Math objects. The constructor property holds a reference to the function that constructs a specific object instance.

For example:
Copy the codeThe code is as follows:

x = new String("Hi");
if ( == String) // Process (condition is true).


or
Copy the codeThe code is as follows:

function MyFunc {
// Function body.
}


Copy the codeThe code is as follows:

y = new MyFunc;
if ( == MyFunc) // Process (condition is true).


For arrays:
Copy the codeThe code is as follows:

y = new Array();