Set JavaScript object values to null?


In order to set object values to null, you need to get all the keys from the object and need to set null. You can use for loop or forEach() loop.

Following is the JavaScript code with name records. We will set the object value to null −

const object=[
   {
      FirstName:"John"
   },
   {
      FirstName:"David"
   },
   {
      FirstName:"Bob"
   }
]
Object.keys(object).forEach(key => object[key]=null);
console.log(object);

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

node fileName.js.

Here, my file name is demo31.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo31.js
[ null, null, null ]

Above, all the values are now set to NULL.

Updated on: 01-Sep-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements