dimanche 3 décembre 2017

Typescript if conditional before returning a new object

I have written a piece of code which will map the value of a data set to a new object.

I would like to check one of the variables in the dataset before assigning it to the new object variable. To do this I will use an if conditional. How can I achieve this using a map in Typescript. The following code will return an error if I try to compile.

If I attempt to do an if conditional at the console.log line, it will only affect the first item in the data set.

return this.UserService.search(url)
.map((data) => {
    console.log(data);
    data.result = <any> data.result.map((user, index) => ({
        // if statement causing error here
        if(user.name === null || user.name === undefined){
            // error with this if condition
        },
        id: user.id,
        name: user.name,
        type: user.type,
        password: user.password,
    }));
    return data;
}).map(data => ({
    meta: { totalItems: data.size },
    data: data.result,
}));

Aucun commentaire:

Enregistrer un commentaire