Home > other >  How to select object with underscore or braces from HTTP Response?
How to select object with underscore or braces from HTTP Response?

Time:10-25

I can select the heading object of this http response:

{data: Array(1), status: 200, statusText: 'OK', headers: {…}, config: {…}, …}
  data: Array(1)
    0:
      heading: "Hello World"

like this:

axios.get('/home')
    .then(res => {
        const $heading = res.data.heading;
    }

How would I select the heading object but for this http response:

{data: Array(1), status: 200, statusText: 'OK', headers: {…}, config: {…}, …}
  data: Array(1)
    0:
      heading_(en): "Hello World"

These attempts produce errors:

const $heading = res.data.heading_(en);
const $heading = res.data['heading_(en)'];

The same thing happens, when I try to select a response with:

heading_en: "Hello World"

by running:

const $heading = res.data.heading_en;

CodePudding user response:

to get it, use res.data[0]['heading_en']

  • Related