Get values of property in a class [duplicate] - javascript

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);
}

Related

Value overwriting in JavaScript [duplicate]

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"

How to call function after ".", javascript [duplicate]

This question already has answers here:
Add method to string class
(6 answers)
Closed 3 years ago.
I have this sample function, and i want to call it in this format :
const myfunc = (string) => {
return string.length
}
console.log("check_length".myfunc())
How can i do that?
Thanks for answers in advance!
The only way is to mutate the prototype of String with a classic function for accessing this.
String.prototype.myfunc = function () {
return this.length;
};
console.log("check_length".myfunc());

How to call function in string on object? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
I have:
Main.children.firstfunction.isEnabled = true;
Main.children.second.isEnabled = true;
Main.children.gsdfgsg.isEnabled = true;
Main.children.other.isEnabled = true;
All of these is working good, but such calls is a lot of, so I have in array:
var names = ['firstfunction', 'second', 'gsdfgsg', 'other'];
And I would like do:
for (var name in names) {
Main.children.name.isEnabled = true;
}
But of course it does not work. How can I improve it?
Use bracket ([]) notation. This will allow you to evaluate the property names dynamically.
Try
for (var name in names) {
Main.children[name].isEnabled = true;
}

Retrieving the variable value to act as a name [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I need an object in Typescript declared like this:
queryParameters = { flagged: true };
Now I would like to have the flagged to be retrieved from a variable name. Something like:
var param = 'flagged';
queryParameters = { ValueOf(param): true };
Any idea ?
Thanks.
Why not use computed property names:
queryParameters = { [param]: true };

Cannot set property in an object in angular js [duplicate]

This question already has answers here:
JavaScript - cannot set property of undefined
(8 answers)
Closed 6 years ago.
$scope.objectData = {};
$scope.objectData[key]["digits"] = set.first+','+set.second+','+set.third+','+set.fourth;
here key is a numerical value.The error is
TypeError: Cannot set property 'digits' of undefined
You need to set the value of $scope.objectData[key] to an object before you can add more keys to it.
$scope.objectData[key] = {};
$scope.objectData[key]['digits'] = 'foo';
You first have to initalize the object $scope.objectData[key] so the right code would be:
$scope.objectData = {};
$scope.objectData[key] = {};
$scope.objectData[key]["digits"] = set.first+','+set.second+','+set.third+','+set.fourth;

Categories