Remove blank attributes from a JavaScript Object
Last Updated :
05 Mar, 2024
Remove blank attributes from a JavaScript object we have to check if the values are null or undefined and then delete the blank attributes with approaches mentioned below.
Removing Blank Attributes from a JavaScript Object
In this approach, we will iterate through the properties of the original object and create a new object, excluding any properties with values that are null or undefined.
Example: Here, the removeBlankAttributes
function is used to create a new object (objectWithoutBlankAttributes
) by excluding properties with null or undefined values from the original object. The resulting object only contains non-blank attributes.
JavaScript
function removeBlankAttributes(obj) {
const result = {};
for (const key in obj) {
if (obj[key] !== null && obj[key] !== undefined) {
result[key] = obj[key];
}
}
return result;
}
const originalObject = {
name: 'John',
age: null,
city: 'New York',
occupation: undefined,
};
const objectWithoutBlankAttributes = removeBlankAttributes(originalObject);
console.log(objectWithoutBlankAttributes);
Output{ name: 'John', city: 'New York' }
Removing Null Values from an Object
This approach defines a function removeNullUndefined
that directly modifies the original object by deleting properties with null or undefined values. It uses a for...in
loop to iterate through the object's properties and checks and deletes those properties with null or undefined values.
Example: Here, the removeNullUndefined
function is applied directly to the sampleObject
, modifying it by deleting properties with null or undefined values. After the operation, sampleObject
contains only the properties with defined values.
JavaScript
function removeNullUndefined(obj) {
for (const key in obj) {
if (obj[key] === null || obj[key] === undefined) {
delete obj[key];
}
}
}
const sampleObject = {
a: 1,
b: null,
c: 3,
d: undefined,
};
removeNullUndefined(sampleObject);
console.log(sampleObject);
Removing Null and Undefined Values from a Nested Object
The removeNestedNullUndefined
function iterates through the properties of the object. If a property has a null or undefined value, it is deleted. If the property is an object, the function recursively calls itself to check and remove null or undefined values within nested objects.
Example: Here, the removeNestedNullUndefined
function is applied to the nestedObject
. It recursively removes properties with null or undefined values, including those within nested objects. The resulting nestedObject
is cleaned from all null and undefined values.
JavaScript
function removeNestedNullUndefined(obj) {
for (const key in obj) {
if (obj[key] === null || obj[key] === undefined) {
delete obj[key];
} else if (typeof obj[key] === 'object') {
removeNestedNullUndefined(obj[key]);
}
}
}
const nestedObject = {
a: 1,
b: null,
c: {
d: 4,
e: undefined,
f: {
g: null,
h: 'hello',
},
},
};
removeNestedNullUndefined(nestedObject);
console.log(nestedObject);
Output{ a: 1, c: { d: 4, f: { h: 'hello' } } }
Removing Null Values from an Object Using reduce()
The removeNullUndefinedWithReduce
function uses the reduce()
method and Object.entries()
to iterate through the properties of the object. It creates a new object, excluding properties with null or undefined values. For nested objects, it recursively applies the same logic. This approach is functional and produces a new object without modifying the original one.
Example: Here, the removeNullUndefinedWithReduce
function is used to create a new object (cleanedObject
) by excluding properties with null or undefined values. The reduce()
method is employed for a functional approach, and the resulting cleanedObject
does not modify the original object. The cleaning process includes nested objects.
JavaScript
function removeNullUndefinedWithReduce(obj) {
return Object.entries(obj).reduce((acc, [key, value]) => {
if (value !== null && value !== undefined) {
acc[key] = typeof value === 'object' ? removeNullUndefinedWithReduce(value) : value;
}
return acc;
}, {});
}
const objectWithNullUndefined = {
a: 1,
b: null,
c: 3,
d: undefined,
e: {
f: null,
g: 'hello',
h: undefined,
},
};
const cleanedObject = removeNullUndefinedWithReduce(objectWithNullUndefined);
console.log(cleanedObject);
Output{ a: 1, c: 3, e: { g: 'hello' } }
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read