How return copy from .find JS? [duplicate] - javascript

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 3 years ago.
I have the following code:
let a = this.menu.getMenuItems().find((item) => item.$entityType === val);
let b = Object.assign({}, a);
this.dictChildren = b.children.map((item) => {
});
First I try to find element in array then create copy.
After I attempted to modify found element using map(), despite
let b = Object.assign({}, a);
It modifies original array.
How map only copied object b?

Object.assign makes a shallow copy and not a deep copy.

Related

Set Object to Object Without Pointer Javascript [duplicate]

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 1 year ago.
In my program, I have 2 objects, A and B, with some code like this:
var a = {"color":"blue"};
var b = a;
a.color = "orange";
console.log(b.color);
By setting B to A, they point to the same memory, and I don't want them to do that. I thought of running a for loop of all the keys and values, setting them individually to B, but there's got to be a better way. Any ideas?
You can use spread syntax to clone the object.
But remember it is just a shallow copy
var a = {
color: "blue"
};
var b = { ...a };
b.color = "red";
console.log(a);
console.log(b);
console.log(a.color);
console.log(b.color);
There are libraries, like lodash, with clone functions, but for a simple, shallow cloning, you can use:
let b = Object.assign({}, a);

How to copy one object to another variable without passing refrence in javascript [duplicate]

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 3 years ago.
I have one object
var a = {b:{c:{d:{e:1}}}}
I want to copy that object to another variable b, without passing a reference, so I try spread operator
var b = {...a}
It is not removing reference at a deep level, as I change the value in the object "a" it also changes the value in the object "b"
a.b.c.d.e = 2
console.log(b.b.c.d.e)
it give output 2 instead of 1
var a = {b:{c:{d:{e:1}}}};
var b = {...a};
a.b.c.d.e = 2;
console.log(b.b.c.d.e)
document.write(b.b.c.d.e)
How can I handle this issue
I've always done this to copy JSON objects. Works great IMO there may be a better way out there.
var a = {b:{c:{d:{e:1}}}};
var b = JSON.parse(JSON.stringify(a));
a.b.c.d.e = 2;
console.log(b.b.c.d.e)
document.write(b.b.c.d.e)

remove property on object if its' key is present on array that contains keys? [duplicate]

This question already has answers here:
One liner to delete multiple object properties
(3 answers)
Closed 3 years ago.
I have an array of keys that can be present as propert on object, if present, I want to remove those properties on it, I know removing a single prop on object like this:
const { 'removedPropOnObj', ...newObj } = obj;
newObj here becomes new object with removed property of removedPropOnObj, i want to do something like that but i have in this case is array of keys like:
['removeKey1','removeKey2','removeKey3']
Help?
You can do that using Object.keys and reduce()
let rmKeys =['removeKey1','removeKey2','removeKey3']
let obj = {
removeKey1:1,
removeKey2:2,
removeKey3:3,
removeKey4:4,
removeKey5:5
}
let newObj = Object.keys(obj).reduce((ac,a) => !rmKeys.includes(a) ?({...ac,[a]:obj[a]}) : ac,{})
console.log(newObj);

Need a copy of value. No need referenced value [duplicate]

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 4 years ago.
I just need to copy the value of data.notes. I have used below code. But still detailsOfCurrentNotes value changes according to the value of data.notes. So could you tell me how to do this?
notes :Note[]
const detailsOfCurrentNotes = Object.assign({}, data.notes);
//here data.notes changes
// detailsOfCurrentNotes also get that value
If the object/array is not circular, you can simply do:
const detailsOfCurrentNotes = JSON.parse(JSON.stringify(data.notes));
If notes is an array, it is:
const detailsOfCurrentNotes = Object.assign([], data.notes);
And shorter syntax is:
const detailsOfCurrentNotes = [...data.notes];
This creates shallow copy of an array.

Clone object created with new in JavaScript [duplicate]

This question already has answers here:
How do I clone a JavaScript class instance?
(6 answers)
Closed 5 years ago.
I am trying to clone an object created with new. The only way I found to do it is:
let tmp = Object.create(instance.__proto__);
let obj = Object.assign(tmp, instance);
This examples works and does the job, but doesn't look like a proper solution. I was wondering if there is a better way to clone the object created with new?
Any help will be appreciated!
It all depends if you want to make a deep or shallow clone.
For the simplicity let's assume that you want to create a shallow clone as in your code.
Then, you can either use shorter version of the code you provided:
let obj = Object.assign({}, instance);
Or use ES6 spread syntax:
let obj = {...instance};
Both ways are correct.

Categories

Resources