【发布时间】:2018-05-17 02:16:08
【问题描述】:
我正在将用户数据的子集映射到精炼数据集的对象。在地图内部,我想检查变量是否为空或未定义,如果是,则将此变量设置为占位符值。
我面临的问题是在 map 中声明 if 语句会导致错误,但是即使 map 可以将索引作为参数,我们如何才能将它与条件语句一起使用?任何见解最受赞赏。
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,
}));
【问题讨论】:
-
什么错误?而且您不会从内部地图内部返回。
-
当然你可以在映射函数中使用
if语句。请提供一些示例输入和所需的输出,否则不清楚您要在这里做什么。 -
请看原帖中的代码。我试图返回{id name type and password}的数据集,但是id首先想在设置之前检查type的值。
标签: javascript typescript dictionary if-statement