如何在JavaScript中为现有数组添加新的值
要向现有的数组添加新的值,可以使用JavaScript splice()方法。
示例
<html>
<head>
<title>JavaScript Array splice Method</title>
</head>
<body>
<script>
var arr = ["orange", "mango", "banana", "sugar", "tea"];
document.write("Original array:"+arr)
var newArr = arr.splice(2, 0, "water");
document.write("<br>After adding new element: " + arr );
</script>
</body>
</html>
输出
Original array:orange,mango,banana,sugar,tea
After adding new element: orange,mango,water,banana,sugar,tea