0

I am fetching multiple arrays of objects for my application. As seen in the picture below the data I get from my backend is one single array containing multiple array of objects. But what I want is one single array containing all objects.

Arrays of array of objects

 //Array of array of objects
 backendData.forEach((data) => {
     //data
 });

I already tried using the spread syntax in the for each loop but without success. I also found a method which takes a variable number of arrays with objects and return one single array of objects:

const merge = (...arrays: any[]) => {
    const merged = {};

    arrays.forEach((data) => data.forEach((o: { id: string }) => Object.assign((merged[o.id] ??= {}), o)));

    return Object.values(merged);
  };

This works when I use the method with static arrays, but I can´t seem to figure out how to convert my data to a single array containing all objects from the other arrays...

1

1 Answer 1

0

const arrays = [[{..}], [{..}],[{..}],[{..}],[{..}].....];

const merged = [].concat.apply([], arrays);

if this not works but static value work for this, that mean browser is braking the data there is only a one array so, you don't have to merge it

for check your exact array GO TO >> developer tools >> networks >> click on your API >> response

but i don't things it a good idea to load that much data there must be a server side pagination

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

Comments

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.