Giả sử sau đây là các con số của chúng tôi với các giá trị đối tượng -
var numberObject = { 2:90 , 6: 98 } Sử dụng Array.from () trong JavaScript -
var fillThePositionValue = Array.from({length: 15}, (value, index) => numberObject[index+ 1] || "novalue") Ví dụ
Sau đây là đoạn mã để lặp các số với các giá trị đối tượng -
var numberObject = { 2:90 , 6: 98 }
console.log("The actual object is=");
console.log(numberObject);
var fillThePositionValue = Array.from({length: 15}, (value, index) => numberObject[index+ 1] || "novalue")
console.log("After filling the value, the actual object is=");
console.log(fillThePositionValue) Để chạy chương trình trên, bạn cần sử dụng lệnh sau -
node fileName.js.
Đây, tên tệp của tôi là demo215.js.
Đầu ra
Kết quả như sau -
PS C:\Users\Amit\JavaScript-code> node demo215.js
The actual object is=
{ '2': 90, '6': 98 }
After filling the value, the actual object is=
[
'novalue', 90,
'novalue', 'novalue',
'novalue', 98,
'novalue', 'novalue',
'novalue', 'novalue',
'novalue', 'novalue',
'novalue', 'novalue',
'novalue'
]