

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to clone a js object except for one key in javascript?
The easiest way to clone an object except for one key would be to clone the whole object and then remove the property that is not required. The cloning, however, can be of 2 types −
- deep clone
- shallow clone
Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.
Example
let innerObj = { a: 'b', c: 'd' } let obj = { x: "test", y: innerObj } // Create a shallow copy. let copyObj = Object.assign({}, obj); // Both copyObj and obj's prop y now refers to the same innerObj. Any changes to this will be reflected. innerObj.a = "test" console.log(obj) console.log(copyObj) This will give the output: { x: 'test', y: { a: 'test', c: 'd' } } { x: 'test', y: { a: 'test', c: 'd' } }
Note that shallow copies do not make clones recursively. It just does it at the top level.
Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection cloned.
Example
let innerObj = { a: 'b', c: 'd' } let obj = { x: "test", y: innerObj } // Create a deep copy. let copyObj = JSON.parse(JSON.stringify(obj)) // Both copyObj and obj's prop y now refers to the same innerObj. Any changes to this will be reflected. innerObj.a = "test" console.log(obj) console.log(copyObj) This will give the output: { x: 'test', y: { a: 'test', c: 'd' } } { x: 'test', y: { a: 'b', c: 'd' } }
Once you get a copy using either of these methods, you can remove the property you don't require using the delete keyword. For example,
Example
let original = { x: 'test', y: { a: 'test', c: 'd' } } let copy = JSON.parse(JSON.stringify(original)) delete copy['x'] console.log(copy) console.log(original) This will give the output: { y: { a: 'test', c: 'd' } } { x: 'test', y: { a: 'test', c: 'd' } }
- Related Questions & Answers
- How to clone a Date object in JavaScript?
- How to clone an object in JavaScript?
- How to set a String as a key for an object - JavaScript?
- Javascript search for an object key in a set
- How to clone an object using spread operator in JavaScript?
- Test for existence of nested JavaScript object key in JavaScript
- Ordering alphabetically in MySQL except for one entry?
- Convert JS array into an object - JavaScript
- De-structuring an object without one key
- How to convert a JS Date to a Python date object?
- Get value for key from nested JSON object in JavaScript
- How to automate this object using JavaScript to set one key on each iteration as null?
- How to select all columns except one in a Pandas DataFrame?
- Checking if a key exists in a JavaScript object
- How to check whether a key exist in JavaScript object or not?