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?

2
  • 1
    You just shouldn't use 4 different variable names, but instead put them in a users array right when you are creating them. Commented May 6, 2016 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? Commented May 6, 2016 at 14:56

3 Answers 3

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().

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

Comments

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.

Comments

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
}

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.