📜  Lodash _.snapshot() 方法

📅  最后修改于: 2022-05-13 01:56:46.335000             🧑  作者: Mango

Lodash _.snapshot() 方法

Lodash是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。

_.snapshot() 方法用于对给定对象进行深度快照或克隆。

句法:

_.snapshot( obj )

参数:此方法需要一个对象来创建一个深度克隆。

返回值:此方法返回克隆的对象。

注意:这在普通 JavaScript 中不起作用,因为它需要安装 lodash contrib 库。 Lodash contrib 库可以使用npm install lodash-contrib –save 安装

示例 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);


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);


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);


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);


输出:

Cloned Object: Object {1: "a", 2: "b"}

示例 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);

输出:

Cloned Object: [1, 2, 3, 4]

示例 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);

输出:

Cloned Object: GeeksforGeeks

示例 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);

输出:

Cloned Object: 10000