js array operation collation

Keywords: Front-end Attribute Javascript Programming

basic operation

Added and improved

var a= new Array(); //Create an array

a[0]=1;//Specify modifications directly
a.push(1); //Add one directly to the last

Delete

//arrayObj.splice(deletePos,deleteCount); //Delete elements of a specified number of deleteCounts starting at the specified location deletePos, returning the removed elements as an array

a.splice[0,1];  //Exception first element

Check and Delete

        for(var i=0;i<a.length;i++){
          if(a[i]==1){
            a.splice(i,1);
          }
        }

Here are some concepts

1. Basic knowledge of arrays

1. Array creation

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 values

Note that although the second method of creating arrays specifies a length, the arrays are actually longer in all cases, that is, even if a length of 5 is specified, elements can still be stored outside the specified length, note that the length will change accordingly.

2. Access to elements of arrays

var testGetArrValue=arrayObj[1]; //Gets the element value of an array
arrayObj[1]= "This is the new value"; //Assign new values to array elements

3. Addition of Array Elements

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
arrayObj.unshift([item1 [item2 [. . . [itemN ]]]]);// Add one or more new elements to the beginning of the array, move elements back automatically, and return the new length of the array
arrayObj.splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);//Inserts one or more new elements into the specified position of the array, and the elements in the insertion position are automatically moved back to return'.

4. Deletion of Array Elements

arrayObj.pop(); //Remove the last element and return its value
arrayObj.shift(); //Remove the previous element and return its value, elements in the array move forward automatically
arrayObj.splice(deletePos,deleteCount); //Removes elements of a specified number of deleteCount s starting at the specified location deletePos, returning the removed elements as an array

5. Interception and merge of arrays

arrayObj.slice(start, [end]); //Returns part of an array as an array, noting that the end corresponding element is not included, and omitting the end copies all elements after the start
arrayObj.concat([item1[, item2[, . . . [,itemN]]]]); //Join multiple arrays (or strings, or a mix of arrays and strings) into one array and return the joined new array

6. Copies of arrays

arrayObj.slice(0); //Returns a copy array of the array, noting that it is a new array, not pointing to
arrayObj.concat(); //Returns a copy array of the array, noting that it is a new array, not pointing to

7. Sorting of Array Elements

arrayObj.reverse(); //Reverse elements (first to last, last to first), returning array addresses
arrayObj.sort(); //Sort array elements and return array addresses

8. Stringing of Array Elements

arrayObj.join(separator); //Returns a string that concatenates each element value of an array separated by a separator.
toLocaleString ,toString ,valueOf: Can be seen as join Special usage, not frequently used

2. Common attribute operations for arrays

1. length attribute

The Length property represents the length of the array, the number of elements in it.Since the index of an array always starts at 0, the upper and lower bounds of an array are 0 and length-1, respectively.Unlike most other languages, the length property of JavaScript arrays is variable, which requires special attention.When the length property is set larger, the state of the entire array does not actually change, only the length property becomes larger; when the length property is set smaller than the original hour, all elements whose index is greater than or equal to the length in the original array are lost.Here is an example of demonstrating how to change the length property:

var arr=[12,23,5,3,25,98,76,54,56,76]; //Defines an array of 10 numbers
alert(arr.length); //Display Array Length 10
arr.length=12; //Increase the length of the array
alert(arr.length); //Display array length has changed to 12
alert(arr[8]); //Displays the value of the ninth element, 56
arr.length=5; //Reduce the length of the array to 5, and elements with an index equal to or greater than 5 are discarded
alert(arr[8]); //Shows that the ninth element has changed to "undefined"
arr.length=10; //Restore Array Length to 10
alert(arr[8]); //Although the length is restored to 10, the ninth element cannot be retracted, showing "undefined"

The code above clearly shows the nature of the length attribute.But not only can the length object be explicitly set, it can also be implicitly modified.You can use an undefined variable in JavaScript, as well as an undefined array element (an element whose index exceeds or equals length), where the value of the length attribute is set to the value of the element index used plus one.For example, the following code:

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

The code also defines an array of 10 numbers, which you can see from the alert statement is 10 in length.The element with an index of 15 is then used and assigned 15, arr[15]=34. Then the length of the array is output with the alert statement, resulting in 16.In any case, this is a surprising feature for developers accustomed to strongly typed programming.In fact, an array created in the form of new Array() has an initial length of 0, and it is the operation on undefined elements that changes the length of the array.

As you can see from the above description, the length attribute is so amazing that it can be used to easily increase or decrease the capacity of arrays.Therefore, a thorough understanding of the length attribute is helpful for flexible use in the development process.

2. prototype Properties

Returns a reference to the object type prototype.The prototype attribute is common to objects.

objectName.prototype

The objectName parameter is the name of the object object object.

Description: Provides a set of basic functions of the object's class with the prototype attribute.A new instance of an object "inherits" the operations that are assigned to the object's prototype.

For array objects, the use of the prototype property is illustrated with the following examples.

Adds a method to an array object that returns the value of the largest element in the array.To do this, declare a function, add it to the Array.prototype, and use it.

function array_max()
 {
   var i,
   max = this[0];
   for (i = 1; i < this.length; i++)
   {
       if (max < this[i])
       max = this[i];
   }
   return max;
 }
 Array.prototype.max = array_max;
 var x = new Array(1, 2, 3, 4, 5, 6);
 var y = x.max();

After the code executes, y saves the maximum value in the array x, or 6.

3. constructor Properties

Represents a function that creates an object.

object.constructor //object is the name of an object or function.

Description: The constructor property is a member of all objects that have a prototype.They include all JScript native objects except Global s and Match objects.The constructor property holds a reference to a function that constructs a specific object instance.

For example:

 x = new String("Hi");
 if (x.constructor == String) // Processing (condition true).

or

function MyFunc {
   // Function body.
}
 y = new MyFunc;
 if (y.constructor == MyFunc) // Processing (condition true).

For arrays:

y = new Array();

Concepts from: https://www.jb51.net/article/59084.htm

Posted by chemoautotroph on Thu, 19 Dec 2019 22:03:25 -0800