1

I want to create 4 new users,

let JonSnow = new User({ id: 1, name: 'Jon Snow', status: Status.user });
let AryaStark = new User({ id: 2, name: 'Arya Star', status: Status.user });
let SansaStark = new User({ id: 3, name: 'Sansa Stark', status: Status.user });
let JoffreyBaretheon = new User({ id: 4, name: 'Joffrey Baretheon', status: Status.user });

In my User class I have a allUser function,

allUsers() {
    let users: IUser[] = [];

    let user: IUser = {
        id: this.id,
        name: this.name,
        status: this.status
    }

    users.push(user);
    users.forEach((user) => console.log(user));
    return users;
}

If I call the the function like so,

JonSnow.allUsers();
AryaStark.allUsers();
SansaStark.allUsers();
JoffreyBaretheon.allUsers();

It calls the allUsers function 4 times, as expected.

How would I store all 4 new users into one array? So I would only need to call the function once to show all users?

Peter Boomsma
  • 8,851
  • 16
  • 93
  • 185
  • 1
    You just shouldn't use 4 different variable names, but instead put them in a users array right when you are creating them. – Bergi May 06 '16 at 14:28
  • Why exactly does each `User` instance have an `allUsers` method which returns an array with only one `IUser`? Is there a case where a `User` might have multiple `IUser` objects? – Aaron Beall May 06 '16 at 14:56

3 Answers3

1

You can have a module/namespace variable (don't export it so it won't be accessible from outside), like so:

let users: IUser[] = [
    new User({ id: 1, name: 'Jon Snow', status: Status.user }),
    new User({ id: 2, name: 'Arya Star', status: Status.user }),
    new User({ id: 3, name: 'Sansa Stark', status: Status.user }),
    new User({ id: 4, name: 'Joffrey Baretheon', status: Status.user })
]

and then you have a simple method:

allUsers(): IUser[] {
    return users.slice(0);
}

Notice that I cloned the users array so that who ever gets a reference to it won't be able to change this inner array, but it's not a must.

But there's no real need to have this method in all classes, you can make it static.
But you can also then make this array a static member of the User class, so:

class User {
    private static users: IUser[] = [];

    constructor() {
        User.users.push(this);
    }

    static allUsers(): IUser[] {
        return User.users.slice(0);
    }
}

The you can get all users: User.allUsers().

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
1

It seems like you are just trying to convert a bunch of User instances into a single array of IUser objects.

What I would do is provide a User class method that returns an IUser object:

public toIUser():IUser {
    return {
        id: this.id,
        name: this.name,
        status: this.status
    }
}

Then simply map your instances using that function:

let allUsers = [JonSnow, AryaStark, SansaStark, JoffreyBaretheon].map(user => user.toIUser());
console.log("all users:", allUsers);

Playground example here.

PS: I would avoid using static variables as your solution.

Community
  • 1
  • 1
Aaron Beall
  • 49,769
  • 26
  • 85
  • 103
0

Declare static User variable as an array:

User.users = []

In User constructor add the current object to the array:

User.users.push(this)

Then in allUsers return the array:

allUsers() {
  return User.users
}
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177