Use the cloneDeep function of Lodash tool library in Javascript to achieve deep copy

Foreword

Recently, I was watching the B station video of the Vue_shop actual project-e-commerce management system (Element-UI) . I saw P172 08. Product added- when I converted goods_cat from an array to a string , I talked about the use of the cloneDeep method of the Lodash tool library . Lodash is a consistent, modular, high-performance JavaScript utility library, its official website address is: https://www.lodashjs.com/ , Github hosting address is: https://github.com/lodash/lodash

Introduction to the use of Loadsh

Download loadsh

installation

  • Browser environment:
<script src="lodash.js"></script>
  • Via npm:
$ npm i -g npm
$ npm i --save lodash
  • Node.js:
// Load the full build.
var _ = require('lodash');
// Load the core build.
var _ = require('lodash/core');
// Load the FP build for immutable auto-curried iteratee-first data-last methods.
var fp = require('lodash/fp');
 
// Load method categories.
var array = require('lodash/array');
var object = require('lodash/fp/object');
 
// Cherry-pick methods for smaller browserify/rollup/webpack bundles.
var at = require('lodash/at');
var curryN = require('lodash/fp/curryN');
  • Note:
    If you want to use Lodash in a REPL environment with Node.js <6, please install n_ .

Why choose Lodash?

Lodash makes JavaScript easier by reducing the difficulty of using arrays, numbers, objects, strings, and so on.
Lodash's modular approach is very suitable for:

  • 1. Traverse array, object and string
  • 2. Operate and check the value
  • 3. Create a function that matches the function

Module format

Lodash provide a variety of ways to build and block formats.

lodash & per method packages
lodash-es, babel-plugin-lodash, & lodash-webpack-plugin
lodash/fp
lodash-amd

Supplementary tools

futil-js is a set of practical tools used to complement lodash.

Further reading

Contribute
release notes
Wiki (update history, road map, etc.)

compatibility

Tested in Chrome 74-75, Firefox 66-67, IE 11, Edge 18, Safari 11-12 and Node.js 8-12 environment.

Use of cloneDeep method in loadsh

Introduction to cloneDeep method

_.cloneDeep(value)

This method is similar to _.clone , except it will copy the value recursively. (Note: also called deep copy).
Add version
1.0.0

Parameter
value ( ): The value to be deep copied.
Return
(
): Returns the copied value.
Examples are as follows:

var objects = [{ 'a': 1 }, { 'b': 2 }];
 
var deep = _.cloneDeep(objects);
console.log(deep[0] === objects[0]);
// => false
Published 131 original articles · Like 38 · Visit 990,000+

Guess you like

Origin blog.csdn.net/ccf19881030/article/details/105452586