编辑
核心代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>添加元素</title> </head> <body> <!-- 在数组 arr 的 index 处添加元素 item。不要直接修改数组 arr,结果返回新的数组 --> <script>function insert(arr, item, index) { var temp = arr.slice(0); temp.splice(index,0,item); return</script> </body> </html>
总结
利用splice直接在指定数组位置插入元素
