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.
//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...
