dimanche 3 décembre 2017

Typescript - if conditrional inside a map

i am mapping a subset of user data to an object of a refined data set. inside the map i want to check if a variable is null or undefined, and if yes, then to set this variable to a placeholder Value.

The issue I am facing is that declaring an if statement inside the map is causing an error, but, even though a map can have an index as a parameter, how can we use it functionally with a conditional Statement? Any insight most appreciated.

return this.UserService.search(url)
.map((data) => {
    console.log(data);
    data.result = <any> data.result.map((user, index) => ({

        // if statement causing error here

        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