admin管理员组文章数量:1310203
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 badges3 Answers
Reset to default 5Edit- 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;
}
本文标签: JavaScript deep copy an array containing nested objectsarrays amp functionsStack Overflow
版权声明:本文标题:JavaScript deep copy an array containing nested objects, arrays & functions? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741843874a2400665.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论