Linux 拨号vps windows公众号手机端

js push方法怎么使用

lewis 6年前 (2019-02-14) 阅读数 13 #程序编程
文章标签 jsjspush

JavaScript中的push()方法用于向数组的末尾添加一个或多个元素,并返回新的数组长度。下面是使用push()方法的示例:

let fruits = ['apple', 'banana'];
let newLength = fruits.push('orange', 'grape');
console.log(fruits);  // 输出: ['apple', 'banana', 'orange', 'grape']
console.log(newLength);  // 输出: 4

在上面的示例中,我们首先创建了一个名为fruits的数组,其中包含两个元素:‘apple’和’banana’。然后,使用push()方法将’orange’和’grape’添加到数组的末尾。最后,使用console.log()函数分别输出了修改后的数组和新的数组长度。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门