- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Looping numbers with object values and push output to an array - JavaScript?
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' ]
Advertisements
To Continue Learning Please Login
Login with Google