您好,登錄后才能下訂單哦!
這篇文章主要介紹JavaScript中往數(shù)組開頭添加新元素的方法有哪些,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
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()
方法在開頭連接兩個(gè)(或更多)數(shù)組。
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中往數(shù)組開頭添加新元素的方法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。