循环使用对象值的数字并将输出推送到数组 - JavaScript?

javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/6 0:12:17

假设以下是带有对象值的数字 −

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

相关文章


有用资源