मान लीजिए निम्नलिखित ऑब्जेक्ट मानों के साथ हमारी संख्याएँ हैं:
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'
]