Lxbster
Lxbster

Reputation: 61

Can not get values of an array from an object

I tried to get values from an array from an object but it doesn't work. Can someone help me.

Here is the JSON parse:

  let userContent = null;
  try {
    userContent = JSON.parse(entity.contactform_content);
  } catch (e) {
    userContent = String(userContent);
  }

Return:

"contactform_id": "118",
"contactform_content": 
"{\"display_name\":\"Peter\",\"user_email\":\"[email protected]\",\"user_nation\":\"be\",
\"category\":[{\"name\":\"Chinese Astrology\",\"value\":\"Chinese Astrology\"},{\"name\":\"Karmic Astrology\",\"value\":\"Karmic Astrology\"},{\"name\":\"Horoscopes\",\"value\":\"Horoscopes\"}],
\"available_time\":\"4 hours\",
\"time_slot\":\"2 hours\"}",   
}

My attempt to get category value:

<TextField
autoComplete="off"
label="Category"
value={userContent.category}
/>

It doesn't show category values

Upvotes: 0

Views: 35

Answers (1)

sonEtLumiere
sonEtLumiere

Reputation: 4562

Try this:

<TextField
  autoComplete="off"
   label="Category"
  value={userContent.category.map(x => x.name)}
/>

Upvotes: 2

Related Questions