Cho object {2:90, 6:98}. Tạo mảng độ dài 15, tại index+1 khớp key thì lấy value, còn lại "novalue".
Mã thực hiện
var numberObject = { 2:90, 6:98 };
var fillThePositionValue = Array.from({length: 15}, (v, i) => numberObject[i+1] || "novalue");
console.log(fillThePositionValue);
Kết quả
[ 'novalue', 90, 'novalue', 'novalue', 'novalue', 98, 'novalue', 'novalue', 'novalue', 'novalue', 'novalue', 'novalue', 'novalue', 'novalue', 'novalue' ]