要在最后添加一个元素,请使用JavaScriptpush()
方法。JavaScript数组push()
方法将给定元素添加到数组的最后,并返回新数组的长度。
您可以尝试运行以下代码以在数组的最后添加元素-
<html> <head> <title>JavaScript Array push Method</title> </head> <body> <script> var numbers = new Array(30, 70, 75, 90); var length = numbers.push(100); document.write("new numbers are : " + numbers ); length = numbers.push(200); document.write("<br />new numbers are : " + numbers ); </script> </body> </html>
输出结果
new numbers are : 30,70,75,90,100 new numbers are : 30,70,75,90,100,200