乐趣区

关于html5:如何在-JS-中的数组开头添加元素

作者:Orkhan Jafarov
译者:前端小智
起源: dev

明天,咱们来一起学习一下如何把元素增加到元素的首个元素。

1.Array.unshift()

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. 应用开展运算符(…)

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()

咱们还能够应用 concat() 办法在结尾连贯两个 (或更多) 数组。

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"]

完~ 我是小智,我要去刷碗了,咱们下期再见~


代码部署后可能存在的 BUG 没法实时晓得,预先为了解决这些 BUG,花了大量的工夫进行 log 调试,这边顺便给大家举荐一个好用的 BUG 监控工具 Fundebug。

原文:https://codingnconcepts.com/j…

交换

文章每周继续更新,能够微信搜寻「大迁世界」第一工夫浏览和催更(比博客早一到两篇哟),本文 GitHub https://github.com/qq449245884/xiaozhi 曾经收录,整顿了很多我的文档,欢送 Star 和欠缺,大家面试能够参照考点温习,另外关注公众号,后盾回复 福利,即可看到福利,你懂的。

退出移动版