您好,登錄后才能下訂單哦!
小編給大家分享一下JavaScript中將元素添加到數組開頭的方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
1.JavaScript主要用來向HTML頁面添加交互行為。 2.JavaScript可以直接嵌入到HTML頁面,但寫成單獨的js文件有利于結構和行為的分離。 3.JavaScript具有跨平臺特性,在絕大多數瀏覽器的支持下,可以在多種平臺下運行。
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"]
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"]
我們還可以使用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"]
看完了這篇文章,相信你對“JavaScript中將元素添加到數組開頭的方法”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。