如何减少if语句的数量
how to reduce amount of if statements
嗨,我正在自学MEAN堆栈,有一个关于如何减少代码中if检查数量的问题。
基本上,这是由用户填写他/她的设置页面,然后点击输入,然后将数据发送到服务器,这样我们就可以更新mongo。
我似乎唯一能让用户编辑某些字段(而不是所有字段)的方法是确保发送到服务器的数据不等于null,但肯定有更好的方法,那就是为每个字段运行if语句。
有问题的代码是这个
//user.username = req.body.username;
if ( age != null) {
user.age = age;
}
if ( bio != null) {
user.bio = bio;
}
if ( location != null) {
user.location = location;
}
if ( team != null) {
user.team = team;
}
if ( tags != null) {
user.tags = tags;
}
if ( email != null) {
user.email = email;
}
客户端代码
$scope.savesettings = function(provider){
var theUser = JSON.parse(localStorage.getItem("User-Data"));
var user = theUser["_id"];
var request = {};
var request = {
user: user,
username: $scope.settings_username,
email: $scope.settings_email,
age: $scope.settings_age,
location: $scope.settings_location,
team: $scope.settings_team,
bio:$scope.settings_bio,
profilebanner: $scope.settings_profilebanner,
avatar: $scope.settings_avatar
};
console.log(request);
//send to server
$http.put('api/social/updatesettings', request).success(function(response){
alertify.success("Your settings have been successfully saved.");
localStorage.clear();
localStorage.setItem('User-Data', JSON.stringify(response));
}).error(function(error){
alertify.error("Hmmm an issue has occured.");
});
};
服务器代码
var User = require('../../datasets/userModel');
module.exports.updatesettings=函数(req,res){
var age = req.body.age;
var bio = req.body.bio;
var location = req.body.location;
var team = req.body.team;
var tags = req.body.tags;
var email = req.body.email;
var profilebanner = req.body.profilebanner;
var avatar = req.body.avatar;
User.findOne({_id: req.body.user}, function (err, user){
//user.username = req.body.username;
if ( age != null) {
user.age = age;
}
if ( bio != null) {
user.bio = bio;
}
if ( location != null) {
user.location = location;
}
if ( team != null) {
user.team = team;
}
if ( tags != null) {
user.tags = tags;
}
if ( email != null) {
user.email = email;
}
user.save(function(err){
if (err){
console.log(err);
res.status(500).send();
//res.json(user);
} else {
console.log("success");
res.json(user);
}
})
});
};
考虑使用Object.assign
。它合并两个或多个对象,后一个对象优先。以下假设data
是一个包含age
、bio
等的对象,而user
是。。。您的user
对象。
var results = Object.assign({}, user, data);
对此有polyfill,如果您碰巧使用jQuery,$.extend
通常会做同样的工作。
您可以将所有属性添加到用户对象中。
var user = {
age: age,
bio: bio,
location: location
}
然后删除空值的键。
for (var key in user) {
if (user[key] === null) {
delete user[key];
}
}
一个函数怎么样?您可以将变量数组传递到该函数中,并使用some
查看其中是否有null
。
function isComplete(args) {
return !args.some(function(el) {
return el === null;
});
}
var age = null;
var bio = 'Something';
var location = null;
isComplete([age, bio, location]); // false
if
不是问题所在。问题是,您向用户显示了他们不应该看到的字段,所以问题出在表示层。
修复那个系列的if
就像在地毯下扫灰尘一样,你似乎想要为你的用户提供角色,所以在你的代码中让它变得清晰易懂。
你可以用服务器端生成的HTML来解决这个问题,比如这样(语法可能有问题,但我希望你能理解):
<% if (user.canSetTeam()) { %>
<input type="text" name="team" />
<% } %>
因此,在HTML中,您将拥有正确的字段。
看看http://expressjs.com/en/guide/using-template-engines.html
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何在ReactJS JSX中执行嵌套的if-else语句
- 自动修复”;语句前缺少分号“;
- 如何在 API 调用后和 if 语句中启用提交按钮
- 是“;x==a||b”;以及“;x==a||x==b”;JavaScript中的等效语句
- 带有多个elseif的Javascript if语句
- 这个条件语句的逻辑有问题
- 如何减少if语句的数量
- 这种减少if语句中声明的变量范围的模式是一种好的做法吗
- 我怎样才能减少 if/else 语句 Javascript
- 如何减少 if-else 语句
- 尽量减少使用“许多重复的if语句”
- 如何减少“如果语句”条件?[减少 if 语句中的条件]
- 减少多个类似的if/else语句
- 是否有方法减少相同if语句的代码
- 尽量减少长mongo语句
- 减少Ctrl中的开关语句为禁用输入字段(AngularJS)
- 减少多个if else语句
- 有没有办法减少这个JavaScript开关语句
- 减少if else语句的数量