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);
Related
This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 1 year ago.
{
pet: {
"0.628": 92694.5,
"8739.836": 96391.94
},
try: {
//same
}
}
When I specify the key, I get the values but i am trying to read all the values without knowing the keys. I have even tried regex, but nothing seems to be working. As you can see i am fairly new. So sorry if this was a stupid question.
console.log(data.pet) // Gives [Object Object]
console.log(data.pet["0.628"])//Gives the value
console.log(data.pet[0])//Gives undefined
I don't see in what context you'd want to access a json object without knowing the keys.
but what you can do is to parse the json file into a javascript object, and call Object.keys() to get the keys of that object
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
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 7 years ago.
I'm building an Elasticsearch search interface. My method is to build the initial query object, and then alter it depending on the user input.
In the filter part of my object, I have
'filter':{
'and':[
{
'term' : {
'terms.region.slug' : 'texas'
}
},
{
...
},
]
}
before I try to alter it, I at least see if I can access it.
console.log( filter.and[0].term.terms.region.slug );
However, I get:
Uncaught TypeError: Cannot read property 'region' of undefined
Maybe because the interpreter is expecting the dot notation to point to levels of an object, whereas this portion of the json going to ES is...erm.. not an object (although it's referencing an object?) This is where I get confused.
EDIT: For reference, this is what the terms object for a single result looks like in Elasticsearch.
This is because the binding you've used (i.e., 'terms.region.slug') is incompatible with dot-notation. Use of dot-notation requires that the binding be parseable as an identifier. However, bracket notation is equivalent to dot-notation and allows any binding to be used.
console.log( filter.and[0].term['terms.region.slug'] );
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I have an object and a few variables I'd like nested inside the variable but for some reason I can only ever get the first nest. Anything after that gives me an error stating that it couldn't read because it was undefined:
var date = 10;
var timestamp = 100;
Que[date] = {timestamp:{"test":"test"}};//this returns {10:{"timestamp":{"test":"test"}}}; for some reason
Que[date][timestamp] = {"test":"test"}; //errors saying, cannot read '100' undefined
console.log(Que);
I'm not sure why this is happening and I'd really like to resolve it with simple means. Btw the Que variable is global inside another script and predeterminietly contains {"10":{"24":{"1":"test"}}}; which is likely why the date variable does work but the timestamp variable doesn't. Any suggestions?
EDIT:
date, and timestamp are both declared outside of the object, however i wish to use them as key's inside of the object..
uppon reading the mod suggested post and implementing what it contained, i ended up with another error
Que = {[date]:{[timestamp]:{"test":"test"}}};//this results in an unexpectd token error located at the [ before date
I think there are more questions to be asked based off of your code snippet, but in your example timesheet is not defined, you can reference the key inside your JSON by using
Que[date]["timesheet"]
To access the object inside. The key that you're looking up with the [] notation should be a string.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
I need to know why this isn't working. My Javascript code accesses a variable in an object. But it appears not to be working, partly because I can't figure out the syntax.
var obj = {
size:"small",
big:false,
thing:true
}
alert(obj[size]);
I'm just not sure if I got the syntax right…
This will work here.
obj.size //returns small
OR
obj["size"] //returns small
OR
var my_var = "size"
obj[my_var] //returns small
You can reference object values either by:
obj["size"]
or
obj.size
However, there is an exception. For instance, if you have following object with a number key: (Note: key is still a string even if it's defined this way):
var obj = {
1: true
};
You can retrieve it's value only by using: obj["1"]
Hence, using obj.1 will cause a syntax error.
Therefore, your code works if you change it to e.g.: alert(obj["size"]); but I prefer to use console.log(obj["size"]); for debugging. At least, if you are playing with node.js as your tags indicates.
Cheers.