Json Data Acess [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I have below json data in the variable .
var links = {
info: {
title: "Informatics "
},
busi: {
title: "Business"
},
lang: {
title: "Language"
}
};
In the code i have the variable named type which could have string as info,busi,lang and that variable is getting as a function argument like
function get_data(type)
{
var data = JSON.parse(links);
// Now i want to access the title of that particular type only I tried to use this but it didnt work
// data.type
// where as if i use the exact word it shows me data like this data.info
}
I want to make the code more generalize rather than sticking to constants like info, busi ,land . Any suggestions how can i make it more generalize ?

To reference dynamic property names, rather than statically, you need square bracket, not dot, syntax.
data.type - looks for a property named 'type'
data[type] - looks for a property whose name is contained within a variable type

Related

Noob Question: How do I access this value in JavaScript? [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 29 days ago.
I've never used JavaScript before and I'm stumped about how to access a particular value in an Object.
The JSON looks like this:
{
"payload":{
"params":{
"switch:0":{
"output":false, **<= trying to get this value ("false")**
}
}
},
}
Node-Red, the tool I'm working with, represents the object like this in its debug pane:
I assumed this was an array and could be accessed like so:
value = msg.payload.params.switch[0].output
But I get an error:
"TypeError: Cannot read property '0' of undefined"
If I try:
value = msg.payload.params.switch
the value is reported as "undefined".
What is the correct way in JavaScript to access the value of "output"? I googled a bunch trying to find an answer, but have been unsuccessful.
Any help is appreciated!
Use bracket notation to access the "switch:0" property since it is not a valid identifier.
let o = {
"payload":{
"params":{
"switch:0":{
"output":false,
}
}
},
}
let res = o.payload.params['switch:0'].output;
console.log(res);

When using a variable to call an object from a JSON file, it doesn't call it at all [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
Using JS in conjunction with a JSON file to turn a room ID into the room information.
function rooms(roomID) {
const roomsDB = require("./rooms.json")
let roomsID = "r".concat("", roomID).toString()
console.log(roomsDB.roomsID)
}
rooms(46)
This is the function I'm using to turn the ID into the info, and the rooms.json file is as follows:
{
"r46":"house"
}
Ideally, this would log house into the console, but I only get undefined.
Why isn't the JSON object being called properly?
To dynamically access an object property by name in a string variable you need to use brackets:
roomsDB[roomsID]
The dot (roomsDB.roomsID) is just like roomsDB["roomsID"], it doesn't substitute the variable.

reading a local variable as an Object property [duplicate]

This question already has answers here:
Create an object with dynamic property names [duplicate]
(2 answers)
Closed 2 years ago.
I am trying to define a new Object type named attributes from an external config file, however I am not sure how to read the property dynamically as even without apostrophes it is being read as a string.
var dynAtt1 = vl.popupAttributes.att1 ;
var attributes = new Object({
dynAtt1 : { title: dynAtt1 }
})
dynAtt1 is being read literally instead of what it is defined as in the config.
You can use a computed property name.
var attributes = {
[dynAtt1] : { title: dynAtt1 }
}

# symbol in the key in JSON is giving error while accessing it [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 4 years ago.
I have following JSON element.
"myObj":[
{
"#type":"xzy",
"data" :"pqr"
}
]
I am accessing a value inside the array using the key as follows
var data = myNode.filter(x => x.#type=='xyz').map(y=>y.data)
But I canot do it becuase of the # symbol in the key. I tried surrounding it with '
var data = myNode.filter(x => x.'#type'=='xyz').map(y=>y.data)
but again it fails. # symbol is valid in JSON. So, I should be able to access it. How can I do this in Javascript? Appreciate your input
Instead of:
var data = myNode.filter(x => x.'#type'=='xyz').map(y=>y.data)
Use this:
var data = myNode.filter(x => x['#type']=='xyz').map(y=>y.data)
Square bracket notation allows access to properties containing special characters and selection of properties using variables.

Javascript syntax clear up [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
var me = new Object();
me.name = "Adithya";
me.age = 15;
for(var prop in me){
console.log(me.prop);
}
When I run this, it prints undefined. I don't understand why it doesnt print the value. When I type
console.log(prop);
it prints
name
age
Doesn't that mean that prop is a a field in me? So shouldn't me.prop(when prop is name) , work like me.name ?
prop is a variable containing a string so you need to use me[prop] like this:
for(var prop in me){
console.log(me[prop]);
}
When your property name is in a variable, you need to use the [prop] syntax which in your case would be me[prop].
FYI, me.prop tries to access a property named "prop". It does not use the contents of the variable prop. You can only use the dot syntax to access a property when the property name is a constant that is known at code parse time and when the characters in the property are legal to use in the Javascript dot syntax. If it's in a variable (or if it contains characters like a . or a space), then you must use the bracket syntax which will fetch the contents of the variable and use that property name.
The syntax you're looking for is me[prop];
me.prop is equivalent to me['prop'] which is not what you want.
notice you didn't assign a value to name or age before when you did me.age and me.name.

Categories