1

I have an object. In my object, there is a key with forward slashes and I would like to access the child of the key with forward slashes pattern

I have tried several approaches to no avail, please help with a solution to access the pattern property. Object is at the bottom

test.attributes./worker/workAssignment/homeOrganizationalUnits/typeCode/codeValue


SyntaxError: Unexpected token /
key = '/worker/workAssignment/homeOrganizationalUnits/typeCode/codeValue'
test.attributes.key


undefined
test.attributes.["key"]

Thrown:
SyntaxError: Unexpected token [
test.attributes.\/worker\/workAssignment\/homeOrganizationalUnits\/typeCode\/codeValue

Thrown:
SyntaxError: Invalid or unexpected token

{
    "attributes": {
        "/worker/workAssignment/homeOrganizationalUnits/typeCode/codeValue": {
            "pattern": "Department"
        }
    },
    "value": {
        "links": [{
            "href": "/codelists/hr/v3/worker-management/departments/WFN/1?$filter=foreignKey eq {payrollGroupCode}",
            "mediaType": "application/json",
            "method": "GET"
        }]
    }
}
Goldfish
  • 576
  • 1
  • 7
  • 22

2 Answers2

1

Either test.attributes[ key ] or test.attributes["/worker/workAssignment/homeOrganizationalUnits/typeCode/codeValue"], both are fine.

Yannick K
  • 4,887
  • 3
  • 11
  • 21
1

You can use the '/' character in the key name for object access as long as it is the value of a variable you use for the lookup.

var key = '///';
var obj = { '///': 'foo' };
obj[key];

> 'foo'

For more information, check out the MDN documentation on Property Accessors

jakemingolla
  • 1,613
  • 1
  • 10
  • 13