Ad

Can I Insert A Value Into Index 6 Of An Array That Has A Length Of Only 4?

- 1 answer

Is that possible? Say there is an array [3, 4] and I want to insert 5 at index 3 to get [3, 4, null, 5], is there a utility to accomplish this?

Ad

Answer

Yes. It's called a "sparse" array.

var ary = [];

ary[5] = 1;

console.log(ary.length); // 6
console.log(ary); // [undefined,undefined,undefined,undefined,undefined,1]
Ad
source: stackoverflow.com
Ad