简体   繁体   English

是否可以从数组中找到 object 并返回 object 而不是他的参考?

[英]is it possible to find an object from an array and return an object not his reference?

is it possible to find an object from an array and return an object not his reference?是否可以从数组中找到 object 并返回 object 而不是他的参考?

let found = this.clonedAllIconsMatching.find((obj) => obj.id == header.id);

the found variable is a reference I need to have an object, I want a copy of the object, and I want to change only the properties of the found object, not the object contained in the array. the found variable is a reference I need to have an object, I want a copy of the object, and I want to change only the properties of the found object, not the object contained in the array.

If you are using ES2018, you can use spread syntax : {...obj} .如果您使用的是 ES2018,则可以使用扩展语法{...obj} Otherwise, you can iterate through and copy the keys and values:否则,您可以遍历并复制键和值:

// obj is the object to clone
var o = {}; // The copy
for(var i in obj){
    o[i] = obj[i];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM