简体   繁体   English

通过迭代具有不同键值对的单个 object 数组来制作 flatList 的最佳方法

[英]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通过迭代具有不同键值对的单个 object 数组来制作 flatList 的最佳方法是什么

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如果索引等于 0,我将返回 whatsapp,否则,我将返回当前索引

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.您可以使用 map 方法,但成本会很高,因为它会创建一个新数组。

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

or you can use forEach Method it或者你可以使用 forEach 方法

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它将所有键名的值和索引为 0 的值存储到对象数组中的最后一个值用于填充数组https://medium.com/@wisecobbler/4-ways-to-populate-an-array-在-javascript-836952aea79f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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