<分区>
我刚刚完成第 1 部分,并注意到没有涉及如何将项目推送到数组的特定位置的方法。例如,如果我想让数组显示
var suits = ["hearts","clubs","Brooks Brothers", "diamonds","spades"]
我如何才能将“Brooks Brothers”插入阵列套装的位置 [2],并将其余 1 向下移动? javascript 中是否有类似于 push 的内置函数可以让我执行此操作?
我想我总是可以,但有一些困难:
function add (item, position){
var length = suits.length;
for(i = length -1; i >= position; i--){
suits[length] = suits[i];
length--;
};
suits[position] = item;
};
add("Brooks Brothers",2) //to add it to the middle