Was it helpful?

Question

Looping numbers with object values and push output to an array - JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

Let’s say the following are our numbers with object values −

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

Use Array.from() in JavaScript −

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

Example

Following is the code to loop numbers with object values −

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)

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo215.js.

Output

The output is as follows −

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'
]
raja
Published on 03-Oct-2020 16:40:05
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top