карта внутри другой карты в React - PullRequest
0 голосов
/ 23 сентября 2019

У меня есть этот формат примечаний json:

{
  "document_tone": {
    "tone_categories": [
      {
        "tones": [
          {
            "score": 0.027962,
            "tone_id": "anger",
            "tone_name": "Colère"
          },
          {
            "score": 0.114214,
            "tone_id": "sadness",
            "tone_name": "Tristesse"
          }
        ],
        "category_id": "emotion_tone",
        "category_name": "Ton émotionnel"
      },
      {
        "tones": [
          {
            "score": 0.028517,
            "tone_id": "analytical",
            "tone_name": "Analytique"
          },
          {
            "score": 0,
            "tone_id": "tentative",
            "tone_name": "Hésitant"
          }
        ],
        "category_id": "language_tone",
        "category_name": "Ton de langage"
      },
      {
        "tones": [
          {
            "score": 0.289319,
            "tone_id": "openness_big5",
            "tone_name": "Ouverture"
          },
          {
            "score": 0.410613,
            "tone_id": "conscientiousness_big5",
            "tone_name": "Tempérament consciencieux"
          },
          {
            "score": 0.956493,
            "tone_id": "emotional_range_big5",
            "tone_name": "Portée émotionnelle"
          }
        ],
        "category_id": "social_tone",
        "category_name": "Ton social"
      }
    ]
  },
  "idMedia": 25840
}

Я пытаюсь отобразить массив объектов tone_categories, а после этого сопоставить массив объектов тонов, чтобы добраться до category_name

Я сделал это:

notes !== undefined && notes !== "" &&  notes.length !== 0 ?
notes.document_tone.tone_categories.map((item, idx) => {
  notes.document_tone.tone_categories[item].tones.map((item2, idx2) => {
    {console.log('notesssss',notes.document_tone.tone_categories[item].tones[item2].category_name)}
  })
}) :
 null}  

Я получаю эту ошибку:

TypeError: Cannot read property 'tones' of undefined

Вы представляете, что я делаю неправильно, любая помощь будет оцененаСпасибо

1 Ответ

1 голос
/ 23 сентября 2019

Чтобы получить category_name, оно должно быть:

const results = notes.document_tone.tone_categories.map(item => {
  return item.category_name
});

( рабочий пример )

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...