let myArray=[11,22,33]; console.log('原數組:',myArray); myArray.push(44,55); console.log('用push在數組后面插入元素:',myArray); myArray.unshift(66,77); console.log('用unshift在數組前面插入元素:',myArray); myArray.splice(2,0,'腎虛少年'); console.log('用splice在數組指定位置插入元素:',myArray);
通過使用push以及unshift即可向數組插入元素,如果要在指定的位置插入元素則可以用splice,splice接收多個參數,分別是索引,要刪除的元素個數,新加的元素(可多個,用逗號隔開);
這樣即可向數組插入元素了。
原版傳送門:https://blog.csdn.net/woshidamimi0/article/details/81154670