JavaScript Acces Object Prop [duplicate] - javascript

This question already has an answer here:
How to access object property beginning with a number (SyntaxError: Unexpected identifier)
(1 answer)
Closed 2 years ago.
I have the following object:
If I try to get the value from it by using let result = object.3h I get the following error
error: `Parsing error: Identifier directly after number`.

Identifiers in Javascript can’t start with a number (You can check the exact rules at the link provided.
When dealing with object properties, you can use string literals for defining properties that won’t follow this rule (Which might also happen when dealing with JSON objects)
In order to access this properties you need to use a property accessor
On your case:
day.rain[‘3h’]

you need to use bracket notation
correct
object["3h"]
incorrect
object.3h
const object = { "3h": 0.44 }
console.log(object["3h"])

Related

Get value from JSON tree but with a number in id ('300px') [duplicate]

This question already has an answer here:
Using dot notation with number passed into function
(1 answer)
Closed 3 years ago.
I would like to get a picture link that is in my JSON tree:
I'm coding in javascript, and I'm trying to grab that data with:
data.response.cases[1].image.300px
But I have the error (In firefox) :
SyntaxError: identifier starts immediately after numeric literal
I have tried like that:
data.response.cases[i-1].image[0]
But the result is 'undefined'
Have you any idea of how to get this link ?
Thanks
First, "cases" is an object, not an array.
Second, the property "300px" starts with a number, therefore it has the be accessed using the following syntax:
data.response.cases['0'].image['300px'];

Use variable to access array results in undefined [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 5 years ago.
I am getting several arrays returned from a GET reponse, which I need to access. Since their names can change based on the request, I have a variable specifing which array to use.
However I always get undefined. See this:
console.log(current); // trips_out_201702
console.log(theResponse.current); // undefined
console.log(theResponse.trips_out_201702); // this works
How can I make theResponse.current work such that it returns what current actually stands for? Why do I get undefined there?
When the property key in an object is a variable you can use the square bracket notation to access that property.
console.log(theResponse[current]);
when acessing with dynamic attribute You should do as
theResponse[current] not theResponse.current
You are trying to get array value using object's way.
You should try this one instant variable['keyName']
Good Luck!

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

Javascript: Altering an object where dot notation is used [duplicate]

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

Do 'variables' have properties? [duplicate]

This question already has answers here:
Why can't I add properties to a string object in javascript?
(2 answers)
Closed 9 years ago.
Do variables have properties?
The obvious answer should be NO. If I try to assign a property to a variable, it should give an error. Right?
If I do something like:
var someVariable = 'Cat';
someVariable.eyes = 'two'; //Gives no error!
alert(someVariable.eyes); // alerts 'undefined' instead of giving an error!
Variables don't have properties, but their values do. (If the value's an object, anyway.)
In this case, you're trying to set the eyes property of the string currently referenced by someVariable.
It won't work in this case, though. Since primitive values don't have properties, JS will convert the primitive string value into an object and set the property on that object, which pretty much immediately gets silently discarded. The end result: the primitive string remains unmodified.
"Variables" don't actually exist (except strictly within the definition of a scope), only objects. And string objects can't have arbitrary properties assigned by default.

Categories