简体   繁体   中英

the best way to make a flatList by iterating over a single object array with different key-value pairs

what's the best way to make a flatList by iterating over a single object array with different key-value pairs

example:

[
  {
    "boteria_chat_aht": "33:55:31",
    "boteria_chat_queue": 2,
    "boteria_chat_tm1r": "00:00:00",
    "boteria_chat_tmr": "00:00:00",
    "boteria_current_chats": 2,
    "boteria_finished_session_percentage": 71.43,
    "boteria_sessions": 7,
    "fbmessenger_chat_aht": "00:00:00",
    "fbmessenger_chat_queue": 0,
    "fbmessenger_chat_tm1r": "00:00:00",
    "fbmessenger_chat_tmr": "00:00:00",
    "fbmessenger_current_chats": 0,
    "fbmessenger_finished_session_percentage": 0,
    "fbmessenger_sessions": 0,
    "inbound_abandoned": 0,
    "inbound_aht": "00:00:00",
    "inbound_asa": "00:00:00",
    "inbound_handled": 0,
    "inbound_queue": 0,
    "inbound_received": 0,
    "mercadolivreperguntas_chat_aht": "00:00:00",
    "mercadolivreperguntas_chat_queue": 0,
    "mercadolivreperguntas_chat_tm1r": "00:00:00",
    "mercadolivreperguntas_chat_tmr": "00:00:00",
    "mercadolivreperguntas_current_chats": 0,
    "mercadolivreperguntas_finished_session_percentage": 0,
    "mercadolivreperguntas_sessions": 0,
    "mercadolivreposvenda_chat_aht": "00:00:00",
    "mercadolivreposvenda_chat_queue": 0,
    "mercadolivreposvenda_chat_tm1r": "00:00:00",
    "mercadolivreposvenda_chat_tmr": "00:00:00",
    "mercadolivreposvenda_current_chats": 0,
    "mercadolivreposvenda_finished_session_percentage": 0,
    "mercadolivreposvenda_sessions": 0,
    "sla": 0,
    "telegram_chat_aht": "00:00:00",
    "telegram_chat_queue": 0,
    "telegram_chat_tm1r": "00:00:00",
    "telegram_chat_tmr": "00:00:00",
    "telegram_current_chats": 0,
    "telegram_finished_session_percentage": 0,
    "telegram_sessions": 0,
    "whatsapp_chat_aht": "29:27:04",
    "whatsapp_chat_queue": 0,
    "whatsapp_chat_tm1r": "01:02:52",
    "whatsapp_chat_tmr": "01:05:59",
    "whatsapp_current_chats": 0,
    "whatsapp_finished_session_percentage": 100,
    "whatsapp_sessions": 18
  }
]

the way I managed to do it was creating a new array of objects receiving these values and separating by each type

example:

[
   {
     Whatsapp...
   }

   {
     telegram...
   }
]

and if the index was equal to 0 I would return whatsapp if not I would return the current index

but I don't think that would be the best way to do it.

can you help me?

Well there are many ways to iterate through array. you can use map method but it will be costly due to it will create a new array.

array.map((element) => { /* your function */ })

or you can use forEach Method it

array.forEach((element) => { /* your function */ })

If you are asking about the different key value than just create a new array and Push the element into the new array.

const newarray = [];
array.forEach((element) => { newarray.push(element)})

It will store all the value with key name and the value with index of 0 to Last-value in object-array For Populating Array https://medium.com/@wisecobbler/4-ways-to-populate-an-array-in-javascript-836952aea79f

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