简体   繁体   中英

Map array inside object

I' trying to get the response of my API and map the peson whit his topic. I map the person whitout problem, but I can t do the same for the topic.. I already did:

const people = props.data.people // get people array
const name = people.map(el => el.person.name) // get the name
const a =  people.map(el => el.topic) 

in a I get the topic, but here returns the two arrays for separated and I need map the person whit the topic, and even then I can 't acces to value and item.. what should I do?

Thanks (sorry for my english)

this is the response:

{
  "people": [
    {
      "person": {
        "id": 3,
        "name": "Arlo Johnston"
      },
      "topic": [
        {
          "item": "dogs",
          "value": 1
        },
        {
          "item": "cats",
          "value": 0
        }
      ]
    },
    {
      "person": {
        "id": 2,
        "name": "Prof. Conner Macejkovic"
      },
      "topic": [
        {
          "item": "dogs",
          "value": 5
        },
        {
          "item": "cats",
          "value": 5
        }
      ]
    }
  ]
}
const nameWithTopics = people.map(el => ({ name: el.person.name, topics: el.topics }))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM