Value overwriting in JavaScript [duplicate] - javascript

This question already has answers here:
Modifying a copy of a JavaScript object is causing the original object to change
(13 answers)
Why can I change a constant object in javascript
(12 answers)
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 12 months ago.
I created an object const with a data and I made other const from the first. But my problem is when I change the second object, my first const also is changed.
Have a method to resolve this?
const user1 = {
name: "samuel"
}
const user2 = user1;
user2.name = "guedes";
console.log(user1); //output: "guedes"
console.log(user2); //output: "guedes"

Yes, you can use es6
const user1 = {
name: "samuel"
}
const user2 = {...user1};
user2.name = "guedes";
console.log(user1); //output: "guedes"
console.log(user2); //output: "guedes"

Related

get JSON property's value when path passed as string: property1.property2.property3 [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 10 months ago.
Supposing I have a JSON object
let dictionary = require('../../es.json');
let obj = JSON.parse(dictionary);
and I have a string variable
let jsonPropertyPath = 'property1.property2.property3'
Now, I would like to get the value of the property property1.property2.property3 of an object obj. Is there any elegant way of doing this?
export const FindTranslation = (jsonPropertyPath: string): string => {
let dictionary = require('../locale/' + testContext.locale + '.json') as JSON;
return eval('dictionary.' + jsonPropertyPath);
};

Get values of property in a class [duplicate]

This question already has answers here:
Loop through an array in JavaScript
(46 answers)
Why is using "for...in" for array iteration a bad idea?
(28 answers)
Closed 2 years ago.
I created a class "basketContent" with two properties (idCamera and lenses) which each have values. I can get several instance of this class. Now I would like to get the value of all the idCamera of each instance. I tried with this, but it's not working for now.
basketContent = localStorage.getItem("basketContent");
console.log(basketContent);
for(idCamera in basketContent){
let itemCamera = cameras.find(cameras => cameras['_id'] == idCamera);
console.log(itemCamera);
}
try this
basketContent = JSON.parse(localStorage.getItem("basketContent")) || {};
console.log(basketContent);
for(idCamera in basketContent){
let itemCamera = cameras.find(cameras => cameras['_id'] == idCamera);
console.log(itemCamera);
}

What are Exceptions to javascript's Object pass by reference? [duplicate]

This question already has answers here:
How does variable assignment work in JavaScript?
(7 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
Consider the Two Snippets
const sayMyName = wizard => {
wizard.name = 'Shazam';
}
let boy = { name: 'Billy' };
sayMyName(boy);
console.log(boy.name);
Since We know that objects in js are passed by reference hence a reference to boy object is assigned a property with value 'Shazam'. Since Object at the stored Reference is changed hence boy's name is changed to 'Shazam'.
const sayMyName = wizard => {
wizard = { name: 'Shazam' };
}
let boy = { name: 'Billy' };
sayMyName(boy);
console.log(boy.name);
Considering above case Here When Boy is passed in sayMyName function why it is behaving as pass by value and boy.name still return 'billy' ?

What exactly it denotes when a key inside a JavaScript object is in square braces [duplicate]

This question already has answers here:
Square Brackets Javascript Object Key
(5 answers)
Closed 4 years ago.
const sample= {
[name]:value
}
What exactly the above JavaScript object represents
This means name is variable
like
let name = 'John';
const sample= {
[name]:value
}
So sample will be like this
sample.John = value
Try this
let name = 'John';
const sample= {
[name]:"I am John"
}
console.log(sample);

Weird javascript code convention with "const" [duplicate]

This question already has answers here:
beginner's: const definition in Redux confusing
(1 answer)
What does curly brackets in the `var { ... } = ...` statements do?
(4 answers)
Closed 5 years ago.
What is this code convention in javascript?
const { navigate } = //whatever
As in, what sense does it make. I saw it in RNs React navigation
https://reactnavigation.org/docs/intro/
It's named destructuring. When you have an object and you want to take only a property of that object, you can get only it by using that convention.
let fullName = {
first: 'John',
last: 'Smith'
}
const { first } = fullName;
You can take a look here for more info's
http://wesbos.com/destructuring-renaming/
It's called destructuring
Example:
var myObject = {a: "what", b: "ever"};
const {a} = myObject;
console.log(a); // will give "what"

Categories