使用物件值進行數字迴圈並將輸出推送到陣列 - JavaScript?


假設以下為具有物件值的物件 −

var numberObject = { 2:90 , 6: 98 }

在 JavaScript 中使用 Array.from() −

var fillThePositionValue = Array.from({length: 15}, (value, index) => numberObject[index+ 1] || "novalue")

示例

以下是使用物件值進行數字迴圈的程式碼 −

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)

要執行上述程式,你需要使用以下命令 −

node fileName.js.

此處,我的檔名是 demo215.js。

輸出

輸出如下 −

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'
]

更新於: 03-Oct-2020

113 次瀏覽

開啟你的職業生涯

透過完成課程來獲得認證

開始
廣告
© . All rights reserved.