Lodash _.snapshot() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.snapshot() method is used to snapshot or clone the given object deeply.
Syntax:
_.snapshot( obj )
Parameters: This method takes an object to create a deep clone.
Return Value: This method returns the cloned object.
Note: This will not work in normal JavaScript because it requires the lodash contrib library to be installed. The Lodash contrib library can be installed using npm install lodash-contrib –save.
Example 1:
Javascript
// Defining the lodash-contrib variable var _ = require( 'lodash-contrib' ); // The given object var given_object = { "a" : 100, "b" : 200, "c" : 300 }; // Using the _.snapshot() method // to create the deep clone var cloned_obj = _.snapshot(given_object); console.log( "Cloned Object: " , cloned_obj); |
Output:
Cloned Object: Object {1: "a", 2: "b"}
Example 2:
Javascript
// Defining the lodash-contrib variable var _ = require( 'lodash-contrib' ); // The given array var given_array = [5, 10, 15, 20, 25]; // Using the _.snapshot() method // to create the deep clone var cloned_arr = _.snapshot(given_array); console.log( "Cloned Array: " , cloned_arr); |
Output:
Cloned Object: [1, 2, 3, 4]
Example 3:
Javascript
// Defining the lodash-contrib variable var _ = require( 'lodash-contrib' ); // The given string var given_string = "GeeksforGeeks" // Using the _.snapshot() method // to create the deep clone var cloned_string = _.snapshot(given_string); console.log( "Cloned String: " , cloned_string); |
Output:
Cloned Object: GeeksforGeeks
Example 4:
Javascript
// Defining the lodash-contrib variable var _ = require( 'lodash-contrib' ); // The given number var given_number = 789434; // Using the _.snapshot() method // to create the deep clone var cloned_num = _.snapshot(given_number); console.log( "Cloned Number: " , cloned_num); |
Output:
Cloned Object: 10000
Please Login to comment...