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
  • 824
  • 1
  • 10
  • 19
  • 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
  • 356,069
  • 52
  • 309
  • 320
0

You can try like this

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

console.log(data._embedded[Object.keys(data._embedded)[0]])
Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46