Lodash是一个JavaScript库,可在underscore.js之上运行。 Lodash帮助处理数组,字符串,对象,数字等。
_.snapshot()方法用于深度快照或克隆给定对象。
用法:
_.snapshot( obj )
参数:此方法采用一个对象来创建深层克隆。
返回值:此方法返回克隆的对象。
注意:由于它需要安装lodash contrib库,因此在正常的JavaScript中将无法使用。可以使用npm install lodash-contrib -save安装Lodash contrib库。
范例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);
输出:
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
相关用法
- underscore.js _.snapshot()用法及代码示例
- Lodash _.method()用法及代码示例
- Lodash _.sneq()用法及代码示例
- Lodash _.toQuery()用法及代码示例
- Lodash _.uniqWith()用法及代码示例
- Lodash _.xorWith()用法及代码示例
- Lodash _.head()用法及代码示例
- Lodash _.remove()用法及代码示例
- Lodash _.pullAt()用法及代码示例
- Lodash _.pullAll()用法及代码示例
- Lodash _.pull()用法及代码示例
- Lodash _.nth()用法及代码示例
- Lodash _.takeRight()用法及代码示例
- Lodash _.take()用法及代码示例
- Lodash _.sortedLastIndex()用法及代码示例
- Lodash _.fromPairs()用法及代码示例
- Lodash _.differenceWith()用法及代码示例
- Lodash _.castArray()用法及代码示例
- Lodash _.cloneDeep()用法及代码示例
- Lodash _.clone()用法及代码示例
- Lodash _.sampleSize()用法及代码示例
- Lodash _.find()用法及代码示例
- Lodash _.zipWith()用法及代码示例
注:本文由纯净天空筛选整理自AshokJaiswal大神的英文原创作品 Lodash _.snapshot() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。