📜  js 添加到数组 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:17.191000             🧑  作者: Mango

代码示例6
const myArray = ['hello', 'world'];

// add an element to the end of the array
myArray.push('foo'); // ['hello', 'world', 'foo']

// add an element to the front of the array
myArray.unshift('bar'); // ['bar', 'hello', 'world', 'foo']

// add an element at an index of your choice
// the first value is the index you want to add at
// the second value is how many you want to delete (0 in this case)
// the third value is the value you want to insert
myArray.splice(2, 0, 'there'); // ['bar', 'hello', 'there', 'world', 'foo']