How can I push an element of array in desired position in JavaScript?
本问题已经有最佳答案,请猛点这里访问。
我用四个元素创建了一个名为"arr"的数组。
1 | var arr = ["apple","ball","cat","elephant"]; |
如何将元素推送到确切的位置(这里,[3])?结果应该是:
1 | var arr = ["apple","ball","cat","dog","elephant"]; |
号
使用拼接:
1 | arr.splice(2, 0,"cat"); |