0

I have a scenario where I have an async request that is pushing data to a child react component. When I log in the "data" in the component, I get back multiple arrays one by one by one...Ideally I would like to avoid rewriting the request in the parent component and just push each array iteration to one large array. Is this possible?

What I currently get

[
    {
       "dates":"Thu, 05/12, 6:25 AM",       
    },
    {
       "dates":"Thu, 05/12, 12:45 PM",       
    }
 ]

 [
    {
       "dates":"Thu, 05/12, 6:25 AM",       
    },
    {
       "dates":"Thu, 05/12, 12:45 PM",       
    },
    {
        "dates":"Thu, 05/12, 2:00 PM",       
     },
     {
        "dates":"Thu, 05/12, 9:00 PM",       
     },
 ]

 [
    {
       "dates":"Thu, 05/12, 6:25 AM",       
    },
    {
       "dates":"Thu, 05/12, 12:45 PM",       
    },
    {
        "dates":"Thu, 05/12, 2:00 PM",       
     },
     {
        "dates":"Thu, 05/12, 9:00 PM",       
     },
     {
        "dates":"Aired on 05/11/22, 5:20 AM",       
     },
     {
        "dates":"Aired on 05/11/22, 12:45 PM",       
     },
     {
        "dates":"Aired on 05/11/22, 1:00 PM",       
     },
 ]

 [
    {
        "dates":"Thu, 05/12, 2:00 PM",       
     },
     {
        "dates":"Thu, 05/12, 9:00 PM",       
     },
     {
        "dates":"Aired on 05/11/22, 5:20 AM",       
     },
     {
        "dates":"Aired on 05/11/22, 12:45 PM",       
     },
     {
        "dates":"Aired on 05/11/22, 1:00 PM",       
     },
 ]

 ....

My desired result would be:

[
    {
       "dates":"Thu, 05/12, 6:25 AM",       
    },
    {
       "dates":"Thu, 05/12, 12:45 PM",       
    },
    {
        "dates":"Thu, 05/12, 6:25 AM",       
     },
     {
        "dates":"Thu, 05/12, 12:45 PM",       
     },
     {
         "dates":"Thu, 05/12, 2:00 PM",       
      },
      {
         "dates":"Thu, 05/12, 9:00 PM",       
      },
      {
        "dates":"Thu, 05/12, 6:25 AM",       
     },
     {
        "dates":"Thu, 05/12, 12:45 PM",       
     },
     {
         "dates":"Thu, 05/12, 2:00 PM",       
      },
      {
         "dates":"Thu, 05/12, 9:00 PM",       
      },
      {
         "dates":"Aired on 05/11/22, 5:20 AM",       
      },
      {
         "dates":"Aired on 05/11/22, 12:45 PM",       
      },
      {
         "dates":"Aired on 05/11/22, 1:00 PM",       
      },
      {
        "dates":"Thu, 05/12, 2:00 PM",       
     },
     {
        "dates":"Thu, 05/12, 9:00 PM",       
     },
     {
        "dates":"Aired on 05/11/22, 5:20 AM",       
     },
     {
        "dates":"Aired on 05/11/22, 12:45 PM",       
     },
     {
        "dates":"Aired on 05/11/22, 1:00 PM",       
     }
 ]

This results in an infinite loop:

for (let i = 0; i < data.length; i++) {
   if (data[0]) data.push(data)
   console.log(data[i])
}

Screenshot of console log in child component. enter image description here

3
  • 1
    What's the structure of the stairs? Is it a big multi-dimensional array of multiple status of objects? Commented May 11, 2022 at 20:40
  • Posted a screenshot of the console log Commented May 11, 2022 at 20:45
  • Put all the arrays inside a big array, then bigArray.flatMap(i => i). Commented May 11, 2022 at 20:48

1 Answer 1

1

If you collect all arrays in one large array (e.g. const arr = []; to define the large array, then arr.push(incomingArray); whenever you get one of the arrays):

With modern JS runtimes, you can use Array.flatMap:

const arr = [["a", "b"], ["c", "d"]];
console.log(arr.flatMap(entry => entry));

With older ones, you can build your own flatmap implementation using reduce quite easily:

const arr = [["a", "b"], ["c", "d"]];
console.log(arr.reduce((aggregated, entry)=> [...aggregated, ...entry]));

Update: full example:

const collectionArray = [];

function onArrayArrived(arr) {
    collectionArray.push(arr);
}

function getFlattenedCollectionArray() {
   return collectionArray.flatMap(entry => entry);
}

onArrayArrived(["a", "b"]);
onArrayArrived(["c", "d"]);
onArrayArrived(["e", "f"]);

console.log(getFlattenedCollectionArray());

Sign up to request clarification or add additional context in comments.

1 Comment

Still no dice. Having an async request in my parent is still resulting in separate arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.