JavaScript Array Type Array Related Attributes and Methods Detailed Explanation

  • 2021-08-12 01:51:54
  • OfStack

Detailed Explanation of Array Array Type

In addition to the object type in ECMAScript, the Array array is the most commonly used type. ECMAScript arrays can store any type of value per item without specifying the length of the array, and can increase the length of the array as the data grows, which is different from arrays in other languages.

1. How to create an array

Array literal mode


var arr = [1,2,3,4,5];//  Simply and directly wrap in brackets to build arrays 

Array constructor


var arr = new Array(1,2,3,4,5);//  Through built-in Array Object to build an array 

2. Detect arrays

The instanceof operator can detect whether an object is an array in the global environment, but if there are more than two global environments when there are multiple frames on the page, this method has some problems. console. log (arr instanceof Array); //true

The Array. isArray () method solves the above problem, and can quickly detect whether the object is an array. console. log (Array. isArray (arr)); //true

3. Conversion method

Use the toString () method to make the array return a string.


var arr = [1,2,3,4];
console.log(arr.toString()); // 1,2,3,4

The toLocaleString () method can also be implemented


var arr = [1,2,3,4];
console.log(arr.toLocaleString()); // 1,2,3,4

4. Stack method

ECMAScript provides a method (stack method) similar to other language arrays for arrays. Stack is a data structure, which mainly pays attention to first in and then out;

The push () method adds elements to the end of the array (multiple entries can be added) The pop () method deletes the last 1 item to the end (only 1 item can be deleted at a time)

 var arr = [1,2,3,4];
 arr.push(5,6,7); //  Add to the end 5,6,7
 console.log(arr); // Output [1,2,3,4,5,6,7]
 arr.pop(); //  Delete the last 1 Items 
 console.log(arr); // Output [1,2,3,4,5,6]

5. Queue method

Queue method pays attention to first in, first out, adding items at the end of the list and deleting items at the beginning.

The push () method mentioned above adds one or more entries at the end shift () method begins with deletion

 var arr = [1,2,3,4];
 arr.push(5,6,7); //  Add to the end 5,6,7
 console.log(arr); // Output [1,2,3,4,5,6,7]
 arr.shift(); //  Delete the 1 Items 
 console.log(arr); // Output [2,3,4,5,6,7]
The pop () method deletes the last item at the end The unshift () method adds one or more elements to the beginning

var arr = [1,2,3,4];
arr.unshift(5,6,7); //  Add to the beginning 5,6,7
console.log(arr); // Output [1,2,3,4,5,6,7]
arr.pop(); //  Delete the last 1 Items 
console.log(arr); // Output [5,6,7,1,2,3]

6. Reordering method

reverse () reverses array change order


var arr = [1,2,3,4,5];
arr.reverse();
console.log(arr); // Output [5,4,3,2,1]

The sort () method compares strings, comparing strings one by one, with the smaller values coming first,


var arr = [1,6,13,40,15];
arr.sort();
console.log(arr); // Output [1, 13, 15, 40, 6]

7. How to operate

The concat () method creates a copy of the current array, adds a new object to the following, and returns a new array. The concat () method does not affect the original array.


 var arr = [1,6,13,40,15];
 var arr2 = arr.concat(2,2,2);
 console.log(arr); // Output [1, 6, 13, 40, 15]
 console.log(arr2); // Output [1, 6, 13, 40, 15,2,2,2]

The slice () method can take one or two arguments. The first argument represents the start position and the second indicates the end position. The slice () method returns an array from the start position to the end position. The slice () method does not affect the original array.


var arr = new Array(1,2,3,4,5);//  Through built-in Array Object to build an array 
0

The splice () method, which I think is the most powerful method in an array! He can pass in three parameters, the starting position of the first parameter, the item to be deleted by the second parameter, and the object to be added or replaced by the third parameter. You can use the splice () method to delete, add, replace, and so on. The splice () method affects the original array.


var arr = new Array(1,2,3,4,5);//  Through built-in Array Object to build an array 
1

var arr = new Array(1,2,3,4,5);//  Through built-in Array Object to build an array 
2

var arr = [1,2,3,4,5]; //  Replace 
var arr2 = arr.splice(1,2,2,2); //  Subscript 1 Start by deleting two items and then replacing them with 2,2
console.log(arr); // Output [1,2,2,4,5]
console.log(arr2); // Output [2,3]  Elements deleted 

8. Position method

There are two ways to find the location of an array. Both the indexOf () and lastIndexOf () methods can take two parameters, the item to find and (optionally) the index to find the location of the starting item. indexOf () is looking from front to back, lastIndexOf () is looking from back to front.


var arr = new Array(1,2,3,4,5);//  Through built-in Array Object to build an array 
4

9. Iterative method

ECMAScript5 provides five iterative methods for arrays, each with two parameters, the function to run on each item and (optionally) the scope object to run that function--the value that affects this. Functions passed into these methods take three formal parameters (item, index, array): the value of the array item, the position of the array object in the array, and the array object itself.

every () returns true if the function returns true for every 1 item given.


var arr = new Array(1,2,3,4,5);//  Through built-in Array Object to build an array 
5

filter () returns an array of true entries, given the function.


var arr = new Array(1,2,3,4,5);//  Through built-in Array Object to build an array 
6

After forEach () has given a function, this method returns no value. Essentially like for cycle 1


var arr = new Array(1,2,3,4,5);//  Through built-in Array Object to build an array 
7

map () returns an array of the results of each function call given a function.


var arr = [1,2,3,4,5];
var arr2 = arr.map(function(item,index,array){
  return item * 2;
});
console.log(arr2); // [2,4,6,8,10] 

some () returns true if any item of the function returns true after a function is given.


var arr = new Array(1,2,3,4,5);//  Through built-in Array Object to build an array 
9

10. Scaling down methods

ECMAScript5 adds two new methods for reducing arrays, reduce () and reduceRight (). Both methods iterate over all the items of the group, reduce () iterates from the first item to the last item, and reduceRight () is the opposite. These two methods can take two parameters, the function to be executed, and (optionally) the initial value as the basis for narrowing. The executing function can pass in four arguments (prev, cur, index, array): the first value, the current value, the index of the array object, and the array object itself.


var arr = [1,2,3,4,5];
var arr2 = arr.reduce(function(prev,cur,index,array){
  return prev * cur;
});
console.log(arr2); // 120  Front 1 Term multiplication 1 Items 

Summarize


Related articles: