Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to modify an object's property in an array of objects in JavaScript ?
Next article icon

How to Remove an Entry by Key in JavaScript Object?

Last Updated : 13 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In JavaScript, objects store data in the form of key-value pairs where the key may be any property of the object. In this article let us see how to remove key-value pairs a by given key in the object.

Table of Content

  • Using the delete operator
  • Using the filter() method
  • Using Destructuring and Object.assign
  • Using Object.fromEntries() with Object.entries()

1. Using the delete operator

When only a single key is to be removed we can directly use the delete operator specifying the key in an object.

Syntax:

delete(object_name.key_name); /* or */ delete(object_name[key_name]);

Example 1: This example describes the above-explained approach to removing a key-value pair from an object.

JavaScript
let myObj = {     Name: "Raghav",     Age: 30,     Sex: "Male",     Work: "Web Developer",     YearsOfExperience: 6,     Organisation: "GeeksforGeeks",     Address: "address--address some value" };  console.log("After removal: "); // Deleting address key delete (myObj.Address); // Or delete(myObj[Address]); console.log(myObj); 

Output
After removal:  {   Name: 'Raghav',   Age: 30,   Sex: 'Male',   Work: 'Web Developer',   YearsOfExperience: 6,   Organisation: 'GeeksforGeeks' } 

Example 2: This example uses a loop to remove a key-value pair from the object.

JavaScript
// Function to delete the keys given in the array function DeleteKeys(myObj, array) {     for (let index = 0; index < array.length; index++) {         delete myObj[array[index]];     }     return myObj; }  // Declaring the object let myObj = {     Name: "Raghav",     Age: 30,     Sex: "Male",     Work: "Web Developer",     YearsOfExperience: 6,     Organisation: "Geeks For Geeks",     Address: "address--address some value" };  // Adding the keys to be deleted in the array let array =     ["Work", "Address", "Organisation", "YearsOfExperience"]; let finalobj = DeleteKeys(myObj, array); console.log("After removal: "); console.log(finalobj); 

Output
After removal:  { Name: 'Raghav', Age: 30, Sex: 'Male' } 

Using the filter() Method

The JavaScript Array filter() method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method. 

Syntax: 

array.filter(callback(element, index, arr), thisValue);

Example: In this example, we will see the use of the filter() method for removing the key-value pair.

JavaScript
let obj = { name: "Joey", age: "30", gender: "Male" }; let newObj = Object.keys(obj)     .filter(key => key != "name")     .reduce((acc, key) => {         acc[key] = obj[key];         return acc;     }, {});  console.log(newObj) 

Output
{ age: '30', gender: 'Male' } 

Using Destructuring and Object.assign

This method involves using object destructuring to exclude specific keys and then reassembling the remaining keys into a new object using Object.assign.

Syntax:

const { keyToRemove, ...rest } = object; const newObject = Object.assign({}, rest);

Example: This example demonstrates how to use destructuring and Object.assign to remove a key-value pair from an object.

JavaScript
let myObj = {     Name: "Raghav",     Age: 30,     Sex: "Male",     Work: "Web Developer",     YearsOfExperience: 6,     Organisation: "GeeksforGeeks",     Address: "address--address some value" };  // Function to delete a key using destructuring and Object.assign function removeKey(obj, keyToRemove) {     const { [keyToRemove]: _, ...rest } = obj;     return Object.assign({}, rest); }  let updatedObj = removeKey(myObj, "Address"); console.log("After removal:"); console.log(updatedObj); 

Output
After removal: {   Name: 'Raghav',   Age: 30,   Sex: 'Male',   Work: 'Web Developer',   YearsOfExperience: 6,   Organisation: 'GeeksforGeeks' } 

Using Object.fromEntries() with Object.entries()

This method involves converting the object into an array of key-value pairs, filtering out the entry to be removed, and then converting the filtered array back into an object.

Example:

JavaScript
let myObj = {     Name: "Raghav",     Age: 30,     Sex: "Male",     Work: "Web Developer",     YearsOfExperience: 6,     Organisation: "GeeksforGeeks",     Address: "address--address some value" };  function removeKey(obj, keyToRemove) {     return Object.fromEntries(         Object.entries(obj).filter(([key]) => key !== keyToRemove)     ); }  let updatedObj = removeKey(myObj, "Address"); console.log("After removal:"); console.log(updatedObj); 

Output
After removal: {   Name: 'Raghav',   Age: 30,   Sex: 'Male',   Work: 'Web Developer',   YearsOfExperience: 6,   Organisation: 'GeeksforGeeks' } 



Next Article
How to modify an object's property in an array of objects in JavaScript ?

L

lokeshpotta20
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-functions
  • JavaScript-Questions

Similar Reads

  • How to get dynamic access to an object property in JavaScript ?
    In JavaScript, an object is a collection of properties, where each property consists of a key-value pair. Objects are a fundamental data type in JavaScript. You can create an object in JavaScript in the following ways: By using the object literal notation, which is a comma-separated list of key-valu
    7 min read
  • How to Remove an Entry by Key in JavaScript Object?
    In JavaScript, objects store data in the form of key-value pairs where the key may be any property of the object. In this article let us see how to remove key-value pairs a by given key in the object. Table of Content Using the delete operatorUsing the filter() methodUsing Destructuring and Object.a
    3 min read
  • How to modify an object's property in an array of objects in JavaScript ?
    Modifying an object's property in an array of objects in JavaScript involves accessing the specific object within the array and updating its property. Using the Array.map() methodUsing the map() method to create a new array by transforming each element of the original array based on a specified func
    5 min read
  • How to compare Arrays of Objects in JavaScript?
    In JavaScript, comparing arrays of objects can be more complex than comparing primitive data types. We will discuss different ways to compare arrays of objects effectively, with detailed code examples and explanations. Syntax: Before going to detail the comparison techniques, let's first understand
    5 min read
  • How to create object properties in JavaScript ?
    JavaScript is built on an object-oriented framework. An object is a collection of properties, where each property links a key to a value. These properties are not in any specific order. The value of a JavaScript property can be a method (function). Object properties can be updated, modified, added,
    4 min read
  • How to create an object with prototype in JavaScript ?
    In this article, we will discuss object creation & prototypes, along with understanding the different ways for object creation & their implementation through the examples. Prototypes are the mechanism by which objects in JavaScript inherit features from another object. A prototype property i
    4 min read
  • How to declare object with computed property name in JavaScript ?
    In this article, we learn how to declare an object with a computed property name. Before beginning this article, we have to know about the javascript objects. Computed Property Names: The ES6 computed property names feature allows us to compute an expression as a property name on an object. Javascri
    2 min read
  • How to add and remove properties from objects in JavaScript ?
    We will try to understand how to add properties to an object and how to add or remove properties from an object in JavaScript. Before we go and see the addition and removal of properties from an object let us first understand the basics of an object in JavaScript. Object: An object in JavaScript is
    6 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
  • JavaScript - Convert Two-Dimensional Array Into an Object
    Here are the different methods to convert the two-dimensional array into an object in JavaScript. 1. Using a for LoopThe simplest way to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs. [GFGTABS] JavaScript const a = [[
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences