This is the 18th day of my participation in the August Genwen Challenge.More challenges in August

preface

“The colors of August are made of gold, bright and precious; The colors of August are brewed with the sun, fragrant and brilliant.”

In the future, I wish you to adjust yourself to the best state, slowly work hard, slowly become better

Arrays can hold any type of data, and ECMAScript arrays can be dynamically resized, which automatically grows as data is added to accommodate new data.

Create an array

Use the Array constructor

Const data = new Array(); Const data = new Array(10); // [] Create an Array of length 10 const data = new Array(10); // create an Array with 3 elements const data = new Array('1', '2 ', '3'); // ["1", 2, "3"] also omit keyword newCopy the code

Array literals

const data = []; // Create an empty array const data = ['1', 2, 'blue']; // Create an array of three elementsCopy the code

Array detection

isArray()

Array.isarray () returns Boolean to check whether the target element is an Array

const o1 = { o: 'aaa' };
const str = '1233344';
const arr = ['1', 2, 'blue'];

Array.isArray(o1)  // false
Array.isArray(str)  // false 
Array.isArray(arr)  // true
Copy the code

An array of conversion

toString()

Array to string

const arr = ['1', 2, 'blue']; arr.toString(); 1, 2, blue "/ /"Copy the code

join()

Concatenate an array as a string

const arr = ['1', 2, 'blue'];
arr.join('*');  // "1*2*blue"
Copy the code

The stack method

The stack is a last-in-first-out (LIFO) data structure, where the latest addition is removed earliest. Stack insertion (push) and removal (pop) occur in one place only — at the top of the stack.

ECMAScript provides push() and pop() methods for arrays to implement stack-like methods.

push()

Insert (push) several elements

const arr = []; arr.push('1', 2, '3'); console.log(arr); [' 1 ', 2, '3');Copy the code

pop()

Removes (pops up) the last element

const arr = ['1', 2, '3']; arr.pop(); console.log(arr); [' 1 ', 2);Copy the code

Queue method

The stack access rule is LIFO(last-in-first-out Last In, First Out). The queue is a first-in-first-out (FIFO) access rule. Queues add elements at the end of the list and remove elements at the front of the list.

Queue methods can be implemented using Shift () and push()

shift()

Removes the element in the first position of the array

const arr = []; arr.push('1', 2, '3'); console.log(arr); / / [' 1 ', 2, '3'); arr.shift(); console.log(arr); / / (2, '3'),Copy the code

conclusion

If this article helped you, please like 👍 and follow ⭐️.

If there are any errors in this article, please correct them in the comments section 🙏🙏.