Home > Web Front-end > JS Tutorial > The array methods often encountered in js learning are summarized well.

The array methods often encountered in js learning are summarized well.

php是最好的语言
Release: 2018-07-27 14:13:46
Original
1357 people have browsed it

Summary of js array method: 1. Insert push at the end of the queue, 2. Delete the last item deleted at the end of the queue and return pop, 3. Insert unshift() at the head of the team, 4. Delete shift() at the head of the team, 5. Array one adds array two concat()

1. Insert push

   var colors = ["red","green"];
   colors.push("black"):
   console.log(colors); //["red","green","black"]
Copy after login

2 at the end of the queue and delete the last item deleted and return pop

   var colors = ["red","green","black"];
   var item = colors.pop();
   console.log(item); //"black"
Copy after login

3. Queue First insert unshift()

   var colors = ["red","green"];
   colors.unshift("black");
   var item = colors.pop();
   console.log(item); //"black"
Copy after login

4. First delete shift()

   var colors = ["red","green","black"];
   colors.shift();
   console.log(colors); //["green","black"]
Copy after login

5. Add array one to array two concat()

   var colors = ["red","green","black"];
   var colors2 = colors.concat("yellow",["blue","brown"]);
   console.log(colors); //["red","green","black"]
   console.log(colors2); //["red","green","black","yellow","blue","brown"]
Copy after login

6. Array interception slice ()
Pass only one parameter: intercept from the subscript of the parameter in the array to the end of the array.

   var colors = ["red","green","black"];
   colors.slice(1); //["green","black"]
   console.log(colors); //["red","green","black"]
Copy after login

Pass two parameters: the first is the starting position of interception, the second is the end position of interception

   var colors = ["red","green","black","yellow","blue","brown"];
   colors.slice(1,3)//从位置1开始,到位置2结束["green","black"];
Copy after login

7, splice() method of array

   有三种用法:
Copy after login
  • Delete: You can delete any number of items by specifying two parameters: the first parameter is the starting position of deletion, and the second parameter is the number of deleted items.

  • Insert: You can insert any number of items into the specified position by providing 3 parameters: starting position, 0 (the number of items to be deleted) and the items to be inserted. For example: splice(2,0,"red","green") will insert the strings "red" and "green" starting from position 2 of the current array.

  • Replacement: You can insert any number of items into the specified position and delete any number of items at the same time. You only need to provide 3 parameters: the starting position, the number of items to be deleted, and the number of items to be deleted. Any number of items inserted. The number of items inserted does not necessarily equal the number of items deleted. For example: splice (2,1,"red","green") will delete the item at position 2 of the current array, and then insert the string starting from position 2.

   var colors = ["red","green","black"];
   var removed = colors.splice(0,1);
   console.log(colors); //["green","black"]
   console.log(removed); //["red"]
    
   removed = colors.splice(1,0,"yellow","orange");
   console.log(colors); //["green","yellow","orange","black"]
   console.log(removed); //[]

   removed = colors.splice(1,1,"red","purple");
   console.log(colors); //["green","red","purple","orange","black"]
   console.log(removed); //["yellow"]
Copy after login

8. Position methods indexOf() and lastIndexOf()

Related articles:

Summary of js array operation methods (must read Article)

Summary of JS array deduplication method

Related videos:

js arrays and objects-Qianfeng Education JS Practical video tutorial

The above is the detailed content of The array methods often encountered in js learning are summarized well.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template