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?

1
  • Or in other words. You want the one and only property (or its content) from _embedded regardless of its name. Commented Feb 19, 2020 at 10:10

2 Answers 2

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]
);

Sign up to request clarification or add additional context in comments.

Comments

0

You can try like this

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

console.log(data._embedded[Object.keys(data._embedded)[0]])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.