How to Change Specific Object Value to Null in JavaScript ?
Last Updated :
12 Aug, 2024
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' }
Using Object.fromEntries() and Object.entries()
This approach leverages Object.entries() to transform the object into an array of key-value pairs, modifies the value associated with a specific key, and then reconstructs the object using Object.fromEntries().
Example:
JavaScript
function setValueToNull(obj, keyToNull) {
const entries = Object.entries(obj);
const updatedEntries = entries.map(([key, value]) =>
key === keyToNull ? [key, null] : [key, value]
);
return Object.fromEntries(updatedEntries);
}
const exampleObject = { name: 'GeeksforGeeks', topic: 'JavaScript' };
const result = setValueToNull(exampleObject, 'topic');
console.log(result);
Output{ name: 'GeeksforGeeks', topic: null }
Similar Reads
How to get Values from Specific Objects an Array in JavaScript ?
In JavaScript, an array is a data structure that can hold a collection of values, which can be of any data type, including numbers, strings, and objects. When an array contains objects, it is called an array of objects. Table of Content Using the forEach() methodUsing the map() methodUsing the filte
2 min read
How to add Key-Value pair to a JavaScript Object?
JavaScript object has key-value pair and it can be of variable length. We first need to declare an object and assign the values to that object for that there can be many methods. Below are the methods to add a key/value pair to a JavaScript object: Table of Content Using Dot NotationUsing Bracket No
3 min read
How to check for null values in JavaScript ?
The null values show the non-appearance of any object value. It is usually set on purpose to indicate that a variable has been declared but not yet assigned any value. This contrasts null from the similar primitive value undefined, which is an unintentional absence of any object value. That is becau
4 min read
How to Compare Objects in JavaScript?
Comparing objects is not as simple as comparing numbers or strings. Objects are compared based on their memory references, so even if two objects have the same properties and values, they are considered distinct if they are stored in different memory locations. Below are the various approaches to co
3 min read
How to check if the value is primitive or not in JavaScript ?
To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data
3 min read
How to check the type of a variable or object in JavaScript ?
In this article, How to Check the Type of a Variable or Object in JavaScript? In JavaScript, the typeof operator is used to determine the typeof an object or variable. JavaScript, on the other hand, is a dynamically typed (or weakly typed) language. This indicates that a variable can have any type o
2 min read
How to remove a key-value pair from JavaScript object?
Removing a key-value pair from an object in JavaScript means deleting a specific property and its corresponding value from the object. This can be achieved using the delete operator, destructuring with the rest operator, or various utility functions to manipulate the object dynamically. There are se
3 min read
How to Replace a value if null or undefined in JavaScript?
In JavaScript, replacing a value if it is null or undefined involves providing a default value to use when the original value is either null or undefined. This ensures the application has valid data, preventing potential errors or unexpected behavior. Here we have some common approaches: Table of Co
2 min read
How to check if the provided value is of the specified type in JavaScript ?
To check if the provided value is of the specified type in JavaScript, we have multiple approaches. Below are the approaches used to check if the provided value is of the specified type in JavaScript: Table of Content Using Object.is() Using TypeOf OperatorApproach: Using Object.is() Object.is() det
3 min read
How to Create Dynamic Values and Objects in JavaScript?
In JavaScript, you can choose dynamic values or variable names and object names and choose to edit the variable name in the future without accessing the array, Dynamic values and objects in JavaScript allow changing, accessing, or creating values and object properties at runtime for flexibility and
3 min read