JavaScript Array Array Method Summary of Recommendation

  • 2021-07-04 18:06:34
  • OfStack

The Array type in JavaScript is often used, and the Array type also provides many methods to meet our requirements. Let's summarize the following 1

1. Method for creating Array

var colors=new Array();

var colors=new Array (3); //Create an array of length 3

var colors = new Array ("red", "blue")//Create an array ["red", "blue"]

Of course, the above new can be omitted, such as var colors = Array ("red");

2. Use array literals directly

var colors=["red","blue","green"];

2. Array Method 1

var colors=["red","blue","green"];

1. Get the length of the array colors. length; //3

2. Access the second item of the array colors [1]; //blue

3. Change item 2 of data colors [1] = "black"; //["red", "black", "green"]

4. Check whether it is an array colors instanceof Array; //true

5. colors. toString (); //The output strings red, blue, green are separated by commas by default

6. colors. join (""); //Customize the output string redbluegreen separated by ""

7. colors. push ("brown")//Add 1 entry to the end of the array

8. colors. pop ()//Delete 1 entry to the end of the array

9. colors. shift ()//Delete entry 1 of the array and get the value

10. colors. unshift ("k1", "k2")//Insert these two entries at the front of the array; ["k1", "k2", "red", "blue", "green"];

11. colors. reverse ()//Flip Array Order

12. colors. sort () or colors. sort ([func]);

13. concat () returns a new array without affecting the original arrays colors. concat () or colors. concat ("k1");

14. slice (begin, end) copies this data from the array subscript begin to end, excluding the subscript end, and if it is slice (begin), it starts from the subscript begin to the end of the array

15. splice

splice (0, 2)//Delete two entries of the array starting with subscript 0

splice (2, 0, "k1", "k2") deletes 0 entries from subscript 2, followed by inserting two entries from here

splice (2, 1, "k1")//Delete 1 entry from subscript 2 and insert 1 entry from here

16. indexOf ("item")//Find an item from the header of the array, and return the subscript value after finding it. If you can't find it, return-1

17. lastIndexOf ("item")//Find an item from the end of the array, and return the subscript value after finding it. If you can't find it, return-1

3. Array Method 2: Iterative Method (ECMAScript5)

1. every (): If the given function is run on every 1 item of the array and true is returned for every 1 item, true is returned (does not affect the original array)


var numbers=[1,2,3,2,1];
// Determine if each number is greater than 2
var flag=numbers.every(function (item,index,array) {
  return item>2;
});

2. filter (): Run the given function for every 1 item in the array and return the item whose function is true (does not affect the original array)


var numbers=[1,2,3,2,1];
// Returns a value greater than 2 Items of 
var array=numbers.filter(function (item,index,array) {
  return item>2;
});

3. forEach (): Executes the given function for every 1 item in the array, and does not return a value (does not affect the original array)


var numbers=[1,2,3,2,1];
// Output per 1 Square of term 
numbers.forEach(function (item,index,array) {
  console.log(item*2);
});

4. map (): Executes a given function for each item of the array and returns an array composed of the results after each function call (without affecting the original array)


var numbers=[1,2,3,2,1];
// Returns every 1 Square of term 
var array=numbers.map(function (item,index,array) {
  return item*item;
});

5. some (): Executes the given function for every 1 item in the array, and returns true if 1 item returns true


var numbers=[1,2,3,2,1];
var flag=numbers.some(function (item,index,array) {
  return item>2
});

3. Array Method 3: Merging Method (ECMAScript5)

1. The reduce () method starts at the first line of the array and iterates one by one to the end

2. The reduceRight () method starts at the last item of the array and walks forward one by one


var numbers=[1,2,3,4,5];
var result=numbers.reduce(function (prev,cur,index,array) {
  //prev : Previous 1 The result of the operation, which is the first of the numbers at the beginning 1 Items 
  //cur: The current item of the array 
  //index: The subscript of the current array 
  //array The array to perform this operation, which is currently numbers
  console.log("prev:"+prev);
  console.log("cur:"+cur);
  console.log("index:"+index);
  console.log("array:"+array);
  console.log("=============");
  return prev+cur;
});

Related articles: