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

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.

Related

Parsing and reformatting an object to a simple javascript array [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 1 year ago.
Suppose I have an object from that has a key and value pair and I want to reformat it into a simple javascript array:
var obj = {STRUCTURE_: '004A006', SEG_ID: '04081591__,0.12,0.25,65850'}
that looks like this:
['004A006', '04081591__,0.12,0.25,65850']
Whats the easiest way to do that?
Use Object.values to get the object's enumerable property values:
var obj = {STRUCTURE_: '004A006', SEG_ID: '04081591__,0.12,0.25,65850'}
const res = Object.values(obj)
console.log(res)

How to get the keyword of a json varible? [duplicate]

This question already has answers here:
How to get all key in JSON object (javascript)
(7 answers)
Closed 2 years ago.
I have a varible, if I print it out, I see this output:
Well, if I'd know the number '118', it would be easy, but in the program where I am using it, I don't know it. So is there any mode to get it without knowing that value?
You could use the Object.keys function to retrieve all keys of your object.
The Object.keys returns an array, you can then access the first element of that array like any other array.
const JSONString = '{"118": {"input1": 6, "input2": 1, "input3": 3}}';
const json = JSON.parse(JSONString);
const keys = Object.keys(json);
console.log(keys[0]);

Why does sorting copied array sorts original array [duplicate]

This question already has answers here:
Copy array by value
(39 answers)
Closed 2 years ago.
q = [3,2,4,1]
let copyQ = q;
copyQ.sort();
console.log(q) // 1,2,3,4 --------> This doesnt make sense.
console.log(copyQ) //1,2,3,4 -----> This makes sense
I had expected that q would remain the same, i.e unsorted as in line 1, because we had sorted copyQ, but it is not the case.
Whats going on there?
sort() function mutates initial array. As soon as your array is copied by link - it is expected. Use spread operator to avoid mutation of initial array. Spread operator will create a copy of your array that will be separate from initial one:
q = [3,2,4,1]
let copyQ = [...q];
copyQ.sort();
console.log(q)
console.log(copyQ)

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)

How return copy from .find JS? [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 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.

Categories

Resources