向J**aScript的数组中添加元素的方法小结

时间:2016.04.21 发布人:jmjk72v13m02

向J**aScript的数组中添加元素的方法小结

已解决问题

谷歌jmjk72v13m02用户在2016.04.21提交了关于“新加坡向J**aScript的数组中添加元素的方法小结”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2025-02-26T14:34:30。希望大家能够帮助她。

详细问题描述及疑问:期待您的答案,我不知道说什么才好,除了谢谢 !

希望以下的回答,能够帮助你。

第1个回答

用户名:yefots  

在数组的开头添加新元素-unshift()
源代码:

<!DOCTYPEhtml><html><body><pid="demo">Clickth331215问答ebuttontoaddelementstothearray.</p><buttononclick="myFunction()">Tryit</button><script>functionmyFun升溶都皮起记信乎督科ction(){varfruits=["Banana","Orange","Apple","Mango"];frui眼其查次ts.unshift("Lemon","Pineapple");varx=document.getElementById("demo");x他业之让读倍等降急密.innerh**TML=fruits;}</script><p><b>Note:</b>T劳需强金轻heunshift()methoddoe五验表沙snotworkproperlyinInte题负击我斤料煤rnetExplorer8andearlier,thevalueswillbeinserted,butther根个句说助独该eturnvalu获农事蒸ewillbe<em>unde层映任利吗先fined</em>.</p></body></html>

测试结果:

Lemon,Pineapple,Banana,Orange,Apple,Mango


在数组的第套谁击学制职2位置添加一个元素-splice()
源代码:

<!DOCTYPEhtml><html><body>​<pid="demo">Clickthebuttontoaddelementstothearray.</p>​<buttononclick="myFu搞界完丰脸查田大缩nction()">Tryit</button>​<s草所守文具游很略cript>functionmyFunction(){varfruits=["Banana","Orange","Apple","Mango"];fruits.spli触根句察掉起离找ce(2,0,"Lemon","Kiwi");varx=document.getElementById("demo");x.innerh**TML=fruits;}</scrip众团持盐调t>​</body></html>

测试结果:

Banan了电写a,Orange,Lemon,Kiwi,Apple,Mango


数组的末尾添加新的元素-pus哪破h()
源代码:

<!DOCTYPEhtml><html><body>​<pid="d特体吸让状现倍emo">Clickthebuttontoaddanewelementtothearray.</p>​<buttononclick="myFunction()">Tryit</bu苦酒连好周响绿影损缩tton>​<script>varfruits=["Banana","Orange","Apple","Mango"];​functionmyFunction(){fruits.push("Kiwi")varx=document.getElementById("demo");x.innerh**TML=fruits;}</script>​</body></html>

测试结果:

Banana,Orange,Apple,Mango,Kiwi