Javascript—此函数中每个变量赋值的if/then替代方法
这段代码是有效的,但我认为有一种更有效的方法来编写它。此函数接收输入到网页表单中的值,并创建一个模型,然后将该模型传递给C并插入数据库。我需要确保,如果给定的表单字段为空,它将传递null,而不是[] 如果listToArray函数针对该字段运行,会发生什么情况 对于每个变量赋值,有没有比使用if/then更有效的方法来写这个Javascript—此函数中每个变量赋值的if/then替代方法,javascript,jquery,performance,Javascript,Jquery,Performance,这段代码是有效的,但我认为有一种更有效的方法来编写它。此函数接收输入到网页表单中的值,并创建一个模型,然后将该模型传递给C并插入数据库。我需要确保,如果给定的表单字段为空,它将传递null,而不是[] 如果listToArray函数针对该字段运行,会发生什么情况 对于每个变量赋值,有没有比使用if/then更有效的方法来写这个 async function newProfile(form) { var profile = {}; if ($('#name').va
async function newProfile(form) {
var profile = {};
if ($('#name').val() === "") { profile.name = null; } else { profile.name = listToArray($('#name').val());}
if ($('#placeOfBirth').val() === "") { profile.placeOfBirth = null; } else { profile.placeOfBirth = listToArray($('#placeOfBirth').val());}
if ($('#position').val() === "") { profile.position = null; } else { profile.position = listToArray($('#position').val());}
//then imagine 15 more variables that have to be written that way
//..
//do some other irrelevant stuff
//..
$('.form').find(':input').prop("disabled", false);
}
您可以直接使用带有三元值的属性
async function newProfile(form) {
var profile = {
name: $('#name').val() === "" ? null : listToArray($('#name').val()),
placeOfBirth: $('#placeOfBirth').val() === "" ? null : listToArray($('#placeOfBirth').val()),
position: $('#position').val() === "" ? null : listToArray($('#position').val())
// more, if necessary
};
$('.form').find(':input').prop("disabled", false);
}
或者使用一组键
async function newProfile(form) {
var profile = Object.fromEntries(['name', 'placeOfBirth', 'position']
.map(v => [v, $(`#${v}`).val() === "" ? null : listToArray($(`#${v}`).val()]));
$('.form').find(':input').prop("disabled", false);
}
创建一个函数来执行重复逻辑
async function newProfile(form) {
const assign = val => val === "" ? null : listToArray(val);
var profile = {};
profile.name = assign($('#name').val());
profile.placeOfBirth = assign($('#placeOfBirth').val());
profile.position = assign($('#position').val());
}
或者更好的办法是将这个答案与尼娜的第一个答案结合起来,我说,这是值得称赞的
async function newProfile(form) {
const assign = val => val === "" ? null : listToArray(val);
var profile = {
name: assign($('#name').val()),
placeOfBirth: assign($('#placeOfBirth').val()),
position: assign($('#position').val())
}
}
如果$and.val部分始终存在
async function newProfile(form) {
const assign = sel => {
const val = $(sel).val();
return val === "" ? null : listToArray(val);
};
var profile = {
name: assign('#name'),
placeOfBirth: assign('#placeOfBirth'),
position: assign('#position')
}
}
我希望下面的方法能奏效。没有测试。您也可以对任何未来的变量进行类似的调用
async function newProfile(form) {
var profile = {};
//if ($('#name').val() === "") { profile.name = null; } else { profile.name = listToArray($('#name').val());}
//if ($('#placeOfBirth').val() === "") { profile.placeOfBirth = null; } else { profile.placeOfBirth = listToArray($('#placeOfBirth').val());}
//if ($('#position').val() === "") { profile.position = null; } else { profile.position = listToArray($('#position').val());}
profile.name = evaluatedata($('#name').val());
profile.placeOfBirth = evaluatedata($('#placeOfBirth').val());
profile.position = evaluatedata($('#position').val());
//then imagine 15 more variables that have to be written that way
//..
//do some other irrelevant stuff
//..
$('.form').find(':input').prop("disabled", false);
}
function evaluatedata(value){
if(value === ""){
return null;
}else
{
return listToArray(value)
}
}
是-数组映射、使用函数等。但这是CodeReview的问题,而不是StackOverflow.profile.name=$'name'。val==?null:listToArray$'name'.val?您还可以提取一个函数以避免重复。这里只需在名称上进行一个[平凡的]循环,因为所有行为都是相同的。请记住,JS允许某些对象[字符串名称]访问属性。如果映射/转换出现偏差,事情会变得稍微有趣,尽管不是很有趣。仍然有太多重复:这个答案很好地揭示了这一点:var profile={name:getListFromFieldname,…}。一行代码可以,但您仍然在重复相对昂贵的$${v}。valSyntax错误:-也就是说,这是我在处理此类共性的任何答案中所期望的那种复制减少。结束语:p此处使用$'position'.val和$valueindex.val进行了不必要的复制,但这是一个良好的开端。一种选择:函数evaluatedataname{var value=$name.val;。}。是的。我错过了,会更新答案的