QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

手机号码,快捷登录

查看: 1518|回复: 0

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

[复制链接]

等级头衔

积分成就    金币 : 2841
   泡泡 : 1516
   精华 : 6
   在线时间 : 1294 小时
   最后登录 : 2024-11-21

丰功伟绩

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

联系方式
发表于 2021-3-18 11:51:52 | 显示全部楼层 |阅读模式
1.Array.unshift()
  X$ R, e/ ^- l- {, X' J+ m
  1. let fruits = ["Apple", "Banana", "Mango"];
  2. fruits.unshift("Orange");
  3. console.log(fruits);
  4. // Prints ["Orange", "Apple", "Banana", "Mango"]
  5. fruits.unshift("Guava", "Papaya");
  6. console.log(fruits);
  7. // Prints ["Guava", "Papaya", "Orange", "Apple", "Banana", "Mango"]
2.使用展开运算符(...)
* F- {5 k( J4 h4 ?
  1. var fruits = ["Apple", "Banana", "Mango"];
  2. var moreFruits = ["Orange", ...fruits];
  3. console.log(moreFruits);
  4. // Prints ["Orange", "Apple", "Banana", "Mango"]
  5. var someoMoreFruits = ["Guava", "Papaya", ...moreFruits];
  6. console.log(someoMoreFruits);
  7. // Prints ["Guava", "Papaya", "Orange", "Apple", "Banana", "Mango"]
  8. console.log(fruits);
  9. // Prints ["Apple", "Banana", "Mango"]
3.使用 Array.concat()
, e( q& S5 o& D9 X8 }; c       我们还可以使用concat()方法在开头连接两个(或更多)数组。4 A; j* J, t9 _+ U8 V& F
  1. var fruits = ["Apple", "Banana", "Mango"];
  2. var moreFruits = ["Orange"];
  3. var someoMoreFruits = ["Guava", "Papaya"];
  4. var allFruits = someoMoreFruits.concat(moreFruits, fruits);
  5. console.log(allFruits);
  6. // Prints ["Guava", "Papaya", "Orange", "Apple", "Banana", "Mango"]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-11-22 01:06

Powered by paopaomj X3.5 © 2016-2025 sitemap

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