I'm developing custom field plugin for Jira Cloud in React.
I have a variable cfDate1
which is stores custom field id as a string like customfield_10072
.
So I want to reach the time data inside my custom field id.
Let's say my data is in jsonData
variable.
I try to reach with jsonData.fields.[cfDate1]
but I get Error: Identifier expected.
I searched a few but I think I couldn't find the right keywords to get some solutions. Is there anyone can help me to figure it out. I need to learn how to reach json data in my json query with a string variable. Thanks already!
JSON Format (jsonData
):
{
"expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations,customfield_10010.requestTypePractice",
"id": "10008",
"self": "https://alimert.atlassian.net/rest/api/2/issue/10008",
"key": "SP-1",
"fields": {
"statuscategorychangedate": "2021-10-11T12:17:41.291 0300",
"customfield_10072": "2021-01-13T02:00:00.000 0300",
"customfield_10073": "2021-01-26T06:30:00.000 0300",
"customfield_10075": "2021-10-14",
"customfield_10076": "2021-10-15" }
CodePudding user response:
It has to be jsonData.fields[cfDate1]
and not jsonData.fields.[cfDate1]
.
There are four ways to access a property of an object1:
Syntax | Dynamic key |
Optional chaining |
. |
[] |
? |
Equivalents2 |
---|---|---|---|---|---|---|
obj.xyz |
No | No | Yes | No | No | obj['xyz'] |
obj[key] |
Yes | No | No | Yes | No | - |
obj?.xyz |
No | Yes | Yes | No | Yes | obj == null ? obj.xyz : undefined obj == null ? obj['xyz'] : undefined |
obj?.[key] ⚠️ |
Yes | Yes | Yes ⚠️ | Yes | Yes | obj == null ? obj[key] : undefined |
As you can see in this table, in most cases you use either .
(for static keys) or []
(for dynamic keys).
Optional chaining adds a ?
. However, in the case of optional chaining and a dynamic key, the possibly unexpected syntax of ?.[]
is used which has both .
and []
3. Note that this is only the case with optional chaining - without optional chaining, the syntax .[]
is not valid.
1: There is also Reflect.get(obj, key)
, and you could stretch that definition to count something involving walking the prototype chain, calling Object.getOwnPropertyDescriptor
and accessing value
or get
as well, if you really wanted to. But for a beginner, these would be, if anything, more of academic interest and rather confusing to include in a list of property access methods for everyday use.
2: For optional chaining, the shown equivalents only apply to the case of no further level of chaining (e.g. obj?.xyz
but not obj?.xyz.abc
). An additional - quite useful - property of the optional chaining operator is that it stops evaluation of the rest of the expression entirely in case the left-hand side is nullish, so that you can write obj?.xyz.abc
instead of obj?.xyz?.abc
without an error Cannot read properties of undefined (reading "abc")
. With the shown equivalent that wouldn't work.
3: The reason for this slightly awkward syntax is that optional chaining was added to the language only recently, and ?[]
couldn't have been used due to ambiguity with the ternary operator and added difficulty in writing parsers to handle this situation. The same goes for ?.()
. See FAQ in the optional chaining proposal for more information.