将值推入数组而不填充(Javascript)

Fre*_*rik 0 javascript arrays push

有没有办法在数组中给出一个特定的索引值。在 PHP 中,我可以这样做 $arr[index] = val;

现在,我用

Array.prototype.insert = function (index, item) {
  this.splice(index, 0, item);
};
Run Code Online (Sandbox Code Playgroud)

var mute = [];
mute.insert(index, 'value');
Run Code Online (Sandbox Code Playgroud)

但是,如果数组为空,则索引仍为 0。如何获得正确的索引?

小智 5

尝试以下 var mute = []; mute[index] = 'value';