-1

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

Malintha
  • 4,512
  • 9
  • 48
  • 82

1 Answers1

1

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.

BlackBeard
  • 10,246
  • 7
  • 52
  • 62