I have a state variable like this:
const [todos, setTodos] = useState([])
I want to put a lot of arrays into it (in an amount unknown to me). This is the code I wrote:
data.forEach(x => {
const userId = x.userId;
const to_to_user = data.filter(d=>d.userId===userId)
setTodos({...todos, [userId]:to_to_user});
});
The problem is that each round of a loop overruns the previous index and at the end of the array there is only the last index. Does anyone have a solution?