I have an array of users of type User = { id: number; name: string; floor: number }, where floor: number is a floor in the company where given user is working.
I want to create an array building of N floors (number is constant), and every building[i] must contain a list of users (an array of User).
So it should look like this:
building = [
1: [ [11, bob, 1], [42, jane, 1], [33, tom, 1] ],
2: [ [14, amir, 2], [35, isaac, 2] ],
...
N: [ [62, jack, N], [93, tobby, N], [21, elisa, N] ],
]
I try to implement idea in the next code:
type User = { id: number; name: string; floor: number }
building: Array<User[]>;
foo() {
this.building = new Array<User[]>(3);
...
for (let i = 0; i < users.length; i++) {
if ( users[i].floor ) {
this.building[users[i].floor].push(users[i]);
}
}
}
But when I run code there is an error: core.mjs:6485 ERROR TypeError: Cannot read property 'push' of undefined
What I missing?
this.building = new Array<User[]>(3);does not create an array with threeUser[]arrays in it. It creates an array with three empty slots in it.