Home  >  Article  >  Web Front-end  >  Arrary method of JS

Arrary method of JS

php中世界最好的语言
php中世界最好的语言Original
2018-03-16 15:45:292593browse

This time I will bring you Arrary method of JS, use Arrary method of JSAttention What are the matters? Below are practical cases. Let’s take a look.

Stack method:

js provides two methods to implement stack-like operations: push(), pop()

The stack is A LIFO (last in first out) data structure,

var arr = [1,2,3,4];
arr.push(5);  // result: [1,2,3,4,5]arr.pop();  // result: [1,2,3,4]

queuemethod:

The queue data structure access rule is FIFO (first in first out). The queue adds items at the end of the list and removes items at the front of the list. The method to achieve this operation is: shift()

var arr = [1,2,3,4];
arr.push(5);  // result: [1,2,3,4,5]arr.shift();  // result: [2,3,4]

Reordering:

reverse() method implements reordering

var arr = [1,2,3,4,5];
arr.reverse();  // [5, 4, 3, 2, 1]
// 另外一种重排序的方法Array.sort()

Splicing:

var arr = [1,2,3];var arr2 = [4,5,6];
arr.concat(arr2);  // result: [1, 2, 3, 4, 5, 6]

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese website Other related articles!

Recommended reading:

Detailed explanation of JavaScript timer

Events and callback functions of JavaScript running mechanism

Detailed explanation of the browser’s multi-threading mechanism

The above is the detailed content of Arrary method of JS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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