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 Swap Array Object Values in JavaScript ?
We have given the array of objects, and our task is to swap the values of the object keys present in the array of objects. Below is an example for a better understanding of the problem statement. Example:Input: arr = [{a: 1, b: 2}, {a:3, b: 4}]Output: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]Explnation: Th
4 min read
How to add Key-Value pair to a JavaScript Object?
A JavaScript object has a 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 ContentUsing Dot NotationUsing Bracket
4 min read
How to check if a value is object-like in JavaScript ?
In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
4 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 change JSON String into an Object in JavaScript ?
In this article we are going to learn how to change JSON String into an object in javascript, JSON stands for JavaScript object notation. It is the plain format used frequently as a communication medium on the internet. It appears close to OOP language like JavaScript but cannot be accessed like Jav
3 min read
How to get a key in a JavaScript object by its value ?
To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.How to get a key in a JavaScript object
4 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.How to remove
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 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 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