How to use Object.assign() In Javascript

Using Object.assign() allows you to create a new object without a specified property by copying all properties except the one you want to remove.

Syntax of object.assign():

const { age, ...rest } = Object.assign({}, details);

Example:

The below code implements the Object.assign() method to remove property from an object.

Javascript
const details = {
    name: 'Alex',
    age: 30,
    country: 'Canada'
};

console.log('orignal object', details)
const { age, ...rest } = Object.assign({}, details);
console.log(rest);

Output
orignal object { name: 'Alex', age: 30, country: 'Canada' }
{ name: 'Alex', country: 'Canada' }

Explanation:

  • The Object.assign({}, details) method creates a shallow copy of the details object. This prevents modification of the original details object.
  • Then, object destructuring is used to extract the ā€œageā€ property from the copied object and assign it to the age variable. The rest of the properties are collected into a new object called rest.
  • As a result, the rest object contains all properties of the original details object except for the ā€œageā€ property.
  • The rest object is then logged to the console, showing the object without the ā€œageā€ property.

How to remove a key-value pair from JavaScript object?

JavaScript object is a powerful data structure that combines keys and values. Sometimes, we need to delete a specific key-value from an object. It can be done using the approaches given below.

How to remove a key-value from JavaScript object ?

There are several methods that can be used to remove a key from a JavaScript object:

Table of Content

  • Using the reduce() and filter() methods
  • Using the delete operator
  • Destructuring with the Rest Operator
  • Using Object.assign()
  • Using Object.fromEntries() and Object.entries()
  • Using _.omit method of Underscore.js library
  • UseCase of Remove a key from JavaScript object

Similar Reads

1. Using the reduce() and filter() methods

The reduce() and the filter() methods of JavaScript can together be used to remove a key from an JavaScript object....

2. Using the delete operator

The delete operator in JavaScript can be used to remove a property (key-value pair) from an object....

3. Destructuring with the Rest Operator

Destructuring an object using the rest operator creates a new object without a specified property, keeping the remaining properties from the original object....

4. Using Object.assign()

Using Object.assign() allows you to create a new object without a specified property by copying all properties except the one you want to remove....

5. Using Object.fromEntries() and Object.entries()

The Object.entries() will be used to convert the object into an array of key-value pairs. Then, we use Array.filter() to exclude the key-value pair with the specified key. Finally, we use Object.fromEntries() to convert the filtered array back into an object....

6. Using _.omit method of Underscore.js library to remove a key from object

The underscore.js is an JavaScript library that can be included in an HTML document through its CDN Link and then you are allowed to use its inbuilt functions....

UseCase of Remove a key from JavaScript object

1. JavaScript Object keys() Method...

Contact Us