Vuex - getters dynamically based on data property [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 years ago.
I want to add a variable to my code, so it calls different functions when I need it but it doesn't work because of the string's quotes. What type should I use or how to strip those quotes from the string?

You could use brackets accessor :
rawData(){
return this.$store.getters['get'+this.dataName]
}
rawData should be a computed property and 'get'+this.dataName has to be in your getters like getTodos

Related

How do I convert a string into a property in javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
Now I am sending length as a string as a function parameter.
function sample(operation){
var str="hello";
console.log(str.operation);
}
sample("length");
I am not allowed to change the way I am sending length(has to be a string).What can I do for this function to give me the expected output?
If you just want to access the property, use bracket notation for your property accessor:
console.log(str[operation])

Accessing the #attributes of a javascript object [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access object properties containing special characters?
(2 answers)
Closed 5 years ago.
I've got some data that's getting returned from an API and converted into a js object in PHP before being passed back to the browser. One of the values i'm trying to retrieve from this object lives in the objects attributes. Here is what the object structure looks like in JS:
Currently I can access all properties I need to by calling object.comments for example or object.email.whatever.
What I can't do is access the objects attributes. Ideally I would like to get to the ID via something like object.#attributes.id but this returns an error.
Is it possible to access an objects attributes and if so how should I go about it?
Thanks
To access properties using dot notation the property must be a valid identifier. If it's not, you have to use brackets:
object['#attributes'].id

How to complete a variable name with another variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 6 years ago.
I have an object that stores 3 variables called text0, text1 and text2 that in turn store one string each. I also have a variable that stores the index of the clicked button, so that if I click on the third button it should fetch the text2.
I was trying something like this:
p.text(myObj.text + btnNumber);
But so far all I get is NaN. Is it even possible to do this way? What am I missing?
Use named indexing of the object to get a variable named property:
p.text(myObj["text" + btnNumber]);
Javascript lets you index properties of an object as if it were a dictionary (indexed via string names).

Getting the value of an object's member through a variable [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
dynamically select a javascript object attribute
(1 answer)
Closed 8 years ago.
I have a javascript object (named resource) used for translating my texts.
resource
.fileNotFound >> "File not found"
.advSearch >> "Advanced search"
Usually I use this like below:
alert (resource.advSearch);
Now I need to access one of the member of this resource object through a variable.
Example:
var trans = "advSearch";
My question: how can I get the translation in my resource object for 'advSearch' contained in the trans variable?
Thanks.
You need to use the bracket notation instead of dot notation as the member operator
resource[trans]
You can use the [] notation to access properties as well.
resource[trans];

How to get the value of a property in javascript which has a dot in it's name? [duplicate]

This question already has answers here:
How to get objects value if its name contains dots?
(4 answers)
How can I access object properties containing special characters?
(2 answers)
Closed 9 years ago.
I have a property with name 'pp.phaseName' in an object 'Config'
Whenever I try to access it like Config.pp.phaseName, it's throwing error.
I've tried using Config.(pp.phaseName), Config."pp.phaseName" etc. but none was working.
Please help me on how to do this.
You have to use the square bracket notation.
Config["pp.phaseName"]

Categories