js中对于数组的操作很常见
let myArray=[11,22,33];
console.log('原数组:',myArray);
myArray.push(44,55);
console.log('用push在数组后面插入元素:',myArray);
结果:[11,22,33,44,55]
myArray.unshift(66,77);
console.log('用unshift在数组前面插入元素:',myArray);
结果:[66,77,11,22,33]
myArray.splice(2,0,'88');
console.log('用splice在数组指定位置插入元素:',myArray);
结果:[11,22,88,33]
通过使用push以及unshift即可向数组插入元素,如果要在指定的位置插入元素则可以用splice,splice接收多个参数,分别是索引,要删除的元素个数,新加的元素(可多个,用逗号隔开);
这样即可向数组插入元素了。