Open In App

How to Change Specific Object Value to Null in JavaScript ?

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We have given an array of objects and our task is to change specific object values to null using JavaScript.

Below is an example for a better understanding of the problem statement.

Example:

Input: { name: 'GeeksforGeeks', topic: 'JavaScript' }
Output: { name: 'GeeksforGeeks', topic: null }

Using Dot Notation

This method uses Dot Notation, where the input object has the properties of ‘name‘ and ‘topic‘. The ‘topic‘ value is explicitly set to null using the dot notation and the modified value is been printed as the output.

Example: The below code uses the Dot Notation to change specific object values to null in JavaScript.

Javascript




// Input obj
let obj =
    { name: 'GeeksforGeeks', topic: 'JavaScript' };
console.log('Input:', obj);
 
// Null using dot notation
obj.topic = null;
 
// Output
console.log('Output:', obj);


Output:

Input: { name:'GeeksforGeeks', topic:'JavaScript' }
Output: { name:'GeeksforGeeks', topic:null }

Using Bracket Notation

This method uses Bracket Notation, where the obj object consists of the properties like ‘name‘ and ‘topic‘. In this object, we are setting the null value to the name object value using bracket notation and printing that modified object as output.

Example: The below code uses the Bracket Notation to change specific object value to null in JavaScript.

Javascript




// Input object
let obj =
    { name: 'GeeksforGeeks', topic: 'JavaScript' };
console.log('Input:', obj);
 
// Value to null using bracket notation
obj['name'] = null;
 
// Output
console.log('Output:', obj);


Output:

Input: { name: 'GeeksforGeeks', topic: 'JavaScript' }
Output: { name:null, topic: 'JavaScript' }

Using Object Destructuring

This method uses Object Destructuring where the obj has name and topic as its properties, the topic property is set to null using object destructuring, creating a new object, and printing a new object as the output.

Example: The below code uses Object Destructuring to change specific object values to null in JavaScript.

Javascript




// Input
let obj =
    { name: 'GeeksforGeeks', topic: 'JavaScript' };
console.log('Input:', obj);
 
// Value to null using object destructuring
obj = { ...obj, topic: null };
 
// Output
console.log('Output:', obj);


Output:

Input: { name: 'GeeksforGeeks', topic: 'JavaScript' }
Output: {name: 'GeeksforGeeks', topic:null }

Using Object.assign() method

This method uses Object.assign() method, where obj has the properties of name and topic. The name property is assigned a value of null using Object.assign() method, creating a new object and printing it as output.

Example: The below code uses Object.assign() method to change specific object values to null in JavaScript.

Javascript




// Input
let obj =
    { name: 'GeeksforGeeks', topic: 'JavaScript' };
console.log('Input:', obj);
 
// Value to null using Object.assign()
obj = Object.assign({}, obj, { name: null });
 
// Output
console.log('Output:', obj);


Output:

Input: { name: 'GeeksforGeeks', topic: 'JavaScript' }
Output: {name: null, topic: 'GeeksforGeeks' }


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads