Fredrik
Fredrik

Reputation: 3293

Push value into array without filling (Javascript)

Is there a way to give a specific index, in an array, a value. In PHP, I could just do this $arr[index] = val;

Right now, I use

Array.prototype.insert = function (index, item) {
  this.splice(index, 0, item);
};

And

var mute = [];
mute.insert(index, 'value');

But, if the array is empty, the index will still be 0. How do I get the right index?

Upvotes: 0

Views: 1100

Answers (1)

Zsolt
Zsolt

Reputation: 170

Try the following var mute = []; mute[index] = 'value';

Upvotes: 5

Related Questions