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 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 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 Detect an Undefined Object Property in JavaScript ? Detecting an undefined object property is the process of determining whether an object contains a certain property, and if it does, whether the value of that property is undefined. This is an important concept in JavaScript programming, as it helps to prevent errors that can occur when attempting to
3 min read
How to invert key value in JavaScript object ? JavaScript is a high-level, interpreted, and dynamically typed client-side scripting language. JavaScript is used to add dynamic features to the static HTML. Everything is an object in JavaScript. Objects in JavaScript can be declared using figure brackets {..} and the objects may comprise certain p
2 min read
How to set default values when destructuring an object in JavaScript ? Destructuring in JavaScript: Destructuring was introduced in ECMA Script 6 version. It is a concept that is used to unpack or free values from arrays and objects into variables. The values corresponding to array elements or object properties get stored in the variables. Example: This example shows t
2 min read
How to read properties of an Object in JavaScript ? Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl
2 min read