1

here is what the object looks like

  {
    approved_for_syndication: 1
    caption: ""
    copyright: ""
    media-metadata: (3) [{…}, {…}, {…}]
    subtype: "photo"  
                    }

how can i use media-metadata without syntax error

for example when i want to use it in a map function

array.map((item)=>{
     return item. //what can i use here?
 })
joseph smith
  • 143
  • 1
  • 10
  • 4
    Put quotes around the key name `"media-metadata"` then to access it use the square bracket notation `obj["media-metadata"]`. I think something like this has been asked before, but I can't find it just yet – evolutionxbox May 15 '21 at 00:06

2 Answers2

3

Ideally you wouldn't do that. I assume you're doing that to match the key as it's stored in some other medium. If that's the case, there are normally options to deserialize/serialize to a camelCase version of that snake-case name.

That said, if you must, you can wrap the key in quotes like this:

copyright: "",
"media-metadata": ...

And you can access the corresponding value with item['media-metadata'].

Nathan Wiles
  • 841
  • 10
  • 30
1

While defining the array media-metadata inside obj it should be wrapped in "" and when mapping over it then you need to use [] like

const obj = {
  approved_for_syndication: 1,
  caption: "",
  copyright: "",
  "media-metadata": [{
    name: "test1"
  }, {
    name: "test2"
  }, {
    name: "test3"
  }],
  subtype: "photo",
};

const result = obj["media-metadata"].map((o) => o.name);
console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42