0

I have the following JavaScript object:

{
  "_embedded": {
    "dealerListItemDToes": [
      {
        ...
      },
      {
        ...
      }
    ]
  }
}

Property called 'dealerListItemDToes' will always be at the given position in the object but its name can vary depending on the HTTP requests.

How can I access the property 'dealerListItemDToes' and retrieve its content without referencing its name?

Ali Celebi
  • 560
  • 1
  • 7
  • 16
  • Or in other words. You want the one and only property (or its content) from `_embedded` regardless of its name. – Andreas Feb 19 '20 at 10:10

2 Answers2

2

Since it's the only property of the _embedded object, you could access the [0]th item in an array of object entries:

const obj = {
  "_embedded": {
    "dealerListItemDToes": [
      {
        // ...
      },
      {
        // ...
      }
    ]
  }
};

console.log(
  Object.entries(obj._embedded)[0]
);
CertainPerformance
  • 313,535
  • 40
  • 245
  • 254
0

You can try like this

let data = {
  "_embedded": {
    "dealerListItemDToes": [{
      "h": 1
    }]
  }
}

console.log(data._embedded[Object.keys(data._embedded)[0]])
Shubh
  • 8,368
  • 4
  • 18
  • 39