最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript deep copy an array containing nested objects, arrays & functions? - Stack Overflow

programmeradmin0浏览0评论

I have an array structured like this and I'm trying to get a copy of it (to modify and use for React setState()). In Python I can use copy.deepcopy() but I can't find an easy way to do this in JavaScript.

notes=[
        {
          contents: "Hello World 1",
          function: console.log,
          children: [
            {
              contents: "Hello World A",
              function: console.log,
              children: []
            },
          ]
        },
        {
          contents: "Hello World 2",
          function: console.log,
          children: []
        }
      ]

I found this article and similar solutions on stackoverflow, but none of them work for me. /@Farzad_YZ/3-ways-to-clone-objects-in-javascript-f752d148054d Two solutions are only a shallow copy, and JSON.parse doesn't work on functions.

I'd like to have a function that can deep copy any array or object containing any arbitrary structure of nested JavaScript datatypes.

I'd rather not reinvent the wheel writing a plex recursive function to traverse and clone everything, is there any existing solution?

I have an array structured like this and I'm trying to get a copy of it (to modify and use for React setState()). In Python I can use copy.deepcopy() but I can't find an easy way to do this in JavaScript.

notes=[
        {
          contents: "Hello World 1",
          function: console.log,
          children: [
            {
              contents: "Hello World A",
              function: console.log,
              children: []
            },
          ]
        },
        {
          contents: "Hello World 2",
          function: console.log,
          children: []
        }
      ]

I found this article and similar solutions on stackoverflow, but none of them work for me. https://medium./@Farzad_YZ/3-ways-to-clone-objects-in-javascript-f752d148054d Two solutions are only a shallow copy, and JSON.parse doesn't work on functions.

I'd like to have a function that can deep copy any array or object containing any arbitrary structure of nested JavaScript datatypes.

I'd rather not reinvent the wheel writing a plex recursive function to traverse and clone everything, is there any existing solution?

Share Improve this question edited Nov 13, 2018 at 3:29 pyjamas asked Nov 13, 2018 at 3:10 pyjamaspyjamas 5,4287 gold badges48 silver badges93 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Edit- You can use the solution below or just import Lodash and use this https://lodash./docs/#cloneDeep


I'm answering my own question with the solution I found. Someone posted this in the ment section of the article I linked and it seems to work

notes=[
        {
          contents: "Hello World 1",
          function: console.log,
          children: [
            {
              contents: "Hello World A",
              function: console.log,
              children: []
            },
          ]
        },
        {
          contents: "Hello World 2",
          function: console.log,
          children: []
        }
      ]

function deepCopy(src) {
  let target = Array.isArray(src) ? [] : {};
  for (let key in src) {
    let v = src[key];
    if (v) {
      if (typeof v === "object") {
        target[key] = deepCopy(v);
      } else {
        target[key] = v;
      }
    } else {
      target[key] = v;
    }
  }

  return target;
}

shortest way if you can not find better answer

var note2 = JSON.parse(JSON.stringify(notes))

but it didnt copy functions

so check

function iterationCopy(src) {
  let target = {};
  for (let prop in src) {
    if (src.hasOwnProperty(prop)) {
      target[prop] = src[prop];
    }
  }
  return target;
}
const source = {a:1, b:2, c:3};
const target = iterationCopy(source);
console.log(target); // {a:1, b:2, c:3}
// Check if clones it and not changing it
source.a = 'a';
console.log(source.a); // 'a'
console.log(target.a); // 1

and

function bestCopyEver(src) {
  return Object.assign({}, src);
}
const source = {a:1, b:2, c:3};
const target = bestCopyEver(source);
console.log(target); // {a:1, b:2, c:3}
// Check if clones it and not changing it
source.a = 'a';
console.log(source.a); // 'a'
console.log(target.a); // 1

from Deep copy using iteration

you should use for loop iterate it and judge item type, when it is object type, use recursion. the function like:

function copy(obj1, obj2) {
  var obj2=obj2||{}; 
  for(var name in obj1) {
    if(typeof obj1[name] === "object") { 
      obj2[name]= (obj1[name].constructor===Array)?[]:{}; 
      copy(obj1[name],obj2[name]);
     } else {
      obj2[name]=obj1[name]; 
   }
 }
  return obj2; 
}
发布评论

评论列表(0)

  1. 暂无评论