-1

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

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.

slacky82
  • 342
  • 4
  • 11
  • 2
    All object values in JavaScript are references, all the time. – Pointy Jan 12 '22 at 13:55
  • Do you mean that you need to clone the object into another instance? It's not really clear what you're trying to do or why. – David Jan 12 '22 at 13:56
  • in OOP "clone" means: make a copy of the object. So if you call a function starting with "clone", you should expect a copy and not the original object. Try calling a function, which does not start with "clone". – ceving Jan 12 '22 at 15:36
  • @David I want a copy of the object found, and I want to change only the properties of this object, not the object contained in the array – slacky82 Jan 13 '22 at 15:11
  • 1
    @slacky82: [How do I correctly clone a JavaScript object?](https://stackoverflow.com/q/728360/328193) – David Jan 13 '22 at 15:23

1 Answers1

0

If you are using ES2018, you can use spread syntax: {...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];
}
2pichar
  • 1,348
  • 4
  • 17