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

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])

Related

Read from JSON where the name is [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
My resultjson's content:
[{"name":"Dummy","id":"780828872962080819","sync_code":123456,"expiration":"2021-04-07T14:03:34.000Z"}]
I need the "name" from the JSON as a String, i tried with JSON.parse(resultjson).name but it didnt work.
It's an array. You need to access an element:
JSON.parse(resultjson)[0 /* or other element */].name

Unable to get object data when used multiple-argument function [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I am unable to get the data of an object
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a.val[0])
}
fun('ab','ef')
It should output 'cd' but it is giving out error in the console
any idea how do i fix this...
Use bracket notation like so:
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a[val[0]])
}
fun('ab','ef')
Your code was trying to get the property named val in a (doesn't exist), then get the first character/item of that value (trying to do this to undefined causes the error).

Access to json variable (node.js) [duplicate]

This question already has answers here:
Iterate through object properties
(31 answers)
How to parse JSON data when the property name is not known in advance?
(2 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
How can I get the value "success" if the value that stands in place "Everlasting hair" is changing?
{
'Everlasting hair' :
{ succes : true,
volume:'12'
}
}
You can discover the property names in the object via for-in or Object.keys. Object.keys, for instance, will give you an array of the object's own, enumerable properties. If you know for sure there will only be one, then:
var success = yourObject[Object.keys(yourObject)[0]].succes;
// Another s here? It's missing in the question -----------^
On cutting-edge JavaScript engines with Object.values (new in ES2017, but polyfillable), as Keith points out if you don't need the name you can use Object.values instead:
var success = Object.values(yourObject)[0].succes;
// Another s here? It's missing in the question -^

JSON - from regex to JSON [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
how do you get objects for events -> performances -> name & occurs_at to this json file https://api.myjson.com/bins/w05x using javascript. Cannot use regex since json API keeps changing format. Thanks
Use the JSON parser
obj = JSON.parse(json_string);
then you access it with
obj.events[event_index].performances[performance_index].performer.name
obj.events[event_index].name
obj.events[event_index].occurs_at
JSFiddle example of listing all the event attributes name and occurs_at.

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