vue 使用lodash實現對象數組深拷貝

<template>
  <div></div>
</template>

<script>
export default {
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let lodash = require('lodash');
      let obj1 = {
        a: 1,
        b: { f: { g: 1 } },
        c: [1, 2, 3],
        h: () => {
          return 123;
        },
        k: undefined
      };
      let obj2 = lodash.cloneDeep(obj1);
      obj2.b.f.g = 2;
      obj2.c = [1, 2];
      obj2.h = 1;
      console.log(obj1);
      console.log(obj2);
    }
  }
};
</script>

在這裏插入圖片描述
如果本篇文章對你有幫助的話,很高興能夠幫助上你。

當然,如果你覺得文章有什麼讓你覺得不合理、或者有更簡單的實現方法又或者有理解不來的地方,希望你在看到之後能夠在評論裏指出來,我會在看到之後儘快的回覆你。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章