- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to automate this object using JavaScript to set one key on each iteration as null?
For this, use Object.keys() and set one key on each iteration as null using a for loop..
Example
Following is the code −
var objectValues = { "name1": "John", "name2": "David", "address1": "US", "address2": "UK" } for (var tempKey of Object.keys(objectValues)) { var inEachIterationSetOneFieldValueWithNull = { ...objectValues, [tempKey]: null }; console.log(inEachIterationSetOneFieldValueWithNull); }
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo294.js.
Output
This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo294.js { name1: null, name2: 'David', address1: 'US', address2: 'UK' } { name1: 'John', name2: null, address1: 'US', address2: 'UK' } { name1: 'John', name2: 'David', address1: null, address2: 'UK' } { name1: 'John', name2: 'David', address1: 'US', address2: null }
- Related Articles
- How to set a String as a key for an object - JavaScript?
- Set JavaScript object values to null?
- JavaScript - Set object key by variable
- How to use null value as key in Java HashMap
- How to clone a js object except for one key in javascript?
- How to access an object value using variable key in JavaScript?
- How to apply function against an accumulator and each key of object in JavaScript?
- Javascript search for an object key in a set
- PHP Object Iteration
- How to set JavaScript object values dynamically?
- How to automate Calendar using Selenium WebDriver for Testing?
- Using object as key for finding values in MongoDB
- JavaScript: How to Create an Object from Key-Value Pairs
- How to create a third object from two objects using the key values in JavaScript?
- JavaScript - Sort key value pair object based on value?

Advertisements