[Vuejs]-Set the default form data keys with an empty string

0👍

This will get you part way to your final solution, you just have to take the concattedUserList and return that as your state.

You must use a class to achieve what you want as that will allow default properties and also a method for returning the full name

class User {
    constructor(public firstName: string = '', public lastName: string = '') {}

    public fullName(): string {
        return `${this.firstName} ${this.lastName}`.trim();
    }
}

// For the first user, we have not passed in a first or second name so it defaults to empty string
const users = [new User(), new User('bob', 'marley'), new User('sandra', 'jackson')];

const concattedUserList = users.map((u) => u.fullName());

console.log(concattedUserList);

returns

[ '', 'bob marley', 'sandra jackson' ]

its probably best to filter the ones which are blank

const concattedUserList = users.map((u) => u.fullName()).filter(n=> n !== '')

Leave a comment