QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

手机号码,快捷登录

查看: 1556|回复: 0

[HTML/CSS/JS] 如何在 JS 中的数组开头添加元素

[复制链接]

等级头衔

积分成就    金币 : 2857
   泡泡 : 1516
   精华 : 6
   在线时间 : 1313 小时
   最后登录 : 2025-1-17

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老活跃会员

联系方式
发表于 2021-3-18 11:51:52 | 显示全部楼层 |阅读模式
1.Array.unshift(): F& W! P( @+ P
let fruits = ["Apple", "Banana", "Mango"];

fruits.unshift("Orange");
console.log(fruits);
// Prints ["Orange", "Apple", "Banana", "Mango"] 
fruits.unshift("Guava", "Papaya");
console.log(fruits);
// Prints ["Guava", "Papaya", "Orange", "Apple", "Banana", "Mango"]
2.使用展开运算符(...)* q  ?1 W) O; o) J! T2 c  U- o
var fruits = ["Apple", "Banana", "Mango"];

var moreFruits = ["Orange", ...fruits];
console.log(moreFruits);
// Prints ["Orange", "Apple", "Banana", "Mango"] 
var someoMoreFruits = ["Guava", "Papaya", ...moreFruits];
console.log(someoMoreFruits);
// Prints ["Guava", "Papaya", "Orange", "Apple", "Banana", "Mango"] 
console.log(fruits);
// Prints ["Apple", "Banana", "Mango"]
3.使用 Array.concat()7 r) R; g+ [2 _0 M
       我们还可以使用concat()方法在开头连接两个(或更多)数组。9 }. K* |: h: I# }8 T; {) N
var fruits = ["Apple", "Banana", "Mango"];
var moreFruits = ["Orange"];
var someoMoreFruits = ["Guava", "Papaya"];

var allFruits = someoMoreFruits.concat(moreFruits, fruits);
console.log(allFruits);
// Prints ["Guava", "Papaya", "Orange", "Apple", "Banana", "Mango"]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号|渝公网安备50010502503914号 )

GMT+8, 2025-1-18 15:59

Powered by paopaomj X3.5 © 2016-2025 sitemap

快速回复 返回顶部 返回列表