Home > Blockchain >  Inserting the value of objects in an object into an array (map takes too long) so I can pass the arr
Inserting the value of objects in an object into an array (map takes too long) so I can pass the arr

Time:08-08

It needs to pass values (name) from the object that is in the object to the array.

I'm doing it now using maps, but since there are a lot of objects, it takes half a minute.

Array of objects that I get using the API:

https://pastebin.com/SPpbzwn3

[
{
    "name": "A",
    "schoolCount": 2
},
{
    "name": "B",
    "schoolCount": 1
},
{
    "name": "C",
    "schoolCount": 1
}]

What I need to get:

["A","B","C"]

How do I now get values from an object into an array:

.then(function (response) {
    response.data.map(({ name }) => {
      setCity((city) => [...city, name]);
    });
  })

CodePudding user response:

Depends. Is:

"setCity" created by useState?? if yes each interation of map is causing render so I am not suprised that it takes time.

I would rather set entire array once it is mapped into object by e.g. setCities to speed it up.

  • Related