
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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' ]
- Related Questions & Answers
- Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?
- Looping through an array in Javascript
- Creating an associative array in JavaScript with push()?
- How to merge an array with an object where values are arrays - JavaScript
- Retrieve key and values from object in an array JavaScript
- Converting a JavaScript object to an array of values - JavaScript
- Recursion in array to find odd numbers and push to new variable JavaScript
- Updating an array with $push in MongoDB
- Mapping array of numbers to an object with corresponding char codes in JavaScript
- Looping in JavaScript to count non-null and non-empty values
- How to return object from an array with highest key values along with name - JavaScript?
- JavaScript: replacing object keys with an array
- Looping through and getting frequency of all the elements in an array JavaScript
- How to push new items to an array inside of an object in MongoDB?
- Filter away object in array with null values JavaScript
Advertisements