How to add an element to an array in es6

青灯夜游
Release: 2022-05-19 15:49:10
Original
7122 people have browsed it

3 methods: 1. Use unshift() to insert an element at the beginning, the syntax is "array object.unshift(element)"; 2. Use push() to insert an element at the end, the syntax "Array object.push(element)"; 3. Use concat(), the syntax is ""array.concat(element)".

How to add an element to an array in es6

The operating environment of this tutorial :windows7 system, ECMAScript version 6, Dell G3 computer.

es6 Three ways to add an element to an array

1 , Use the unshift() function

The unshift() function can insert elements at the beginning of the array. This function can append one or more parameter values ​​to the head of the array:

array.unshift(元素1, 元素2, ..., 元素X)
Copy after login

The first parameter Element1 is the new element 0 of the array, the second parameter Element2 is the new element 1, and so on, and finally returns the length of the array after adding the element.

Let’s take a closer look at the following example:

var a = [0];  //定义数组
console.log(a);  //返回[0]

a.unshift(1);  //增加1个元素
console.log(a);  //返回[1,0]
Copy after login

How to add an element to an array in es6

2. Use the push() function

## The #push() method can append one or more parameter values ​​to the end of the array and return the length of the array after adding elements.

array.push(元素1, 元素2, ..., 元素X)
Copy after login

Let’s take a look at the following example:

var a = [0];  //定义数组
console.log(a);  //返回[0]

a.push(2);  //增加1个元素
console.log(a);  //返回[0,2]
Copy after login

How to add an element to an array in es6

3. Use the concat() function

The concat() method can connect two or more arrays and will use one or more arrays as parameters. Elements of multiple arrays are added to the end of the specified array.

You can also insert one or more given elements, and all the parameters passed can be added to the end of the array in order.


var a = [1,2,3,4,5];  //定义数组
console.log(a);  
var b = a.concat(6);  //为数组a连接1个元素
console.log(b);
Copy after login
The output result is:

How to add an element to an array in es6##Explanation: The concat() method will create and return a new array instead of adding new elements to the original one; but unshift The () method will add elements based on the original array.

[Related recommendations:

javascript video tutorial

, web front-end]

The above is the detailed content of How to add an element to an array in es6. 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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!