9951 explained code solutions for 126 technologies


javascript-lodashHow do I clone an object using Lodash in JavaScript?


Lodash is a JavaScript library that provides many utility functions for working with objects. To clone an object using Lodash, you can use the _.cloneDeep() function. It will create a deep copy of the object, meaning that the new object will contain copies of all of the original object's properties.

Example

const _ = require('lodash');

const obj = {
  name: 'John',
  age: 30
};

const clone = _.cloneDeep(obj);

console.log(clone);
// Output: { name: 'John', age: 30 }

The _.cloneDeep() function takes a single argument, the object to be cloned. It returns a new object that is a deep copy of the original.

Code explanation

  • const _ = require('lodash'); - This line imports the Lodash library.
  • const obj = { name: 'John', age: 30 }; - This line creates an object to be cloned.
  • const clone = _.cloneDeep(obj); - This line uses the _.cloneDeep() function to clone the object.
  • console.log(clone); - This line logs the cloned object.

Helpful links

Edit this code on GitHub