这是我的代码:
Search.prototype.makeQuery = function (data) {
let result = {};
if (data.orderId) {
result["order_id"] = data.orderId;
}
if (data.userMobileNumber) {
result["user.Number"] = {$regex : data.userMobileNumber}
}
if (data.userFullName) {
result["user.Name"] = {$regex: data.userFullName}
}
return result;
};
我想要的只是找到更好的方法来优化我的代码并减少代码中的if
条件。有什么建议吗?
当你将它包装成一个函数并用if
输入data
时,你可以避免输入destructuring。
在这种情况下将if
包装成函数的优点是你可以简单地测试它,它是可重用的并且易于阅读
Search.prototype.makeQuery = function (data) {
let result = {}
let {orderId, userMobileNumber, userFullName} = data
setObjectValue(orderId, result, "order_id", orderId)
setObjectValue(userMobileNumber, result, "user.Number", {$regex : userMobileNumber})
setObjectValue(userFullName, result, "user.Name", {$regex: userFullName})
return result;
}
function setObjectValue(condition, object, key, value) {
if(condition) {
object[key] = value
}
}
function makeQuery (data) {
let result = {}
let {orderId, userMobileNumber, userFullName} = data
setObjectValue(orderId, result, "order_id", orderId)
setObjectValue(userMobileNumber, result, "user.Number", {$regex : userMobileNumber})
setObjectValue(userFullName, result, "user.Name", {$regex: userFullName})
return result;
}
function setObjectValue(condition, object, key, value) {
if(condition) {
object[key] = value
}
}
let data = {
orderId: 1,
userMobileNumber: "016875447895",
userFullName: "John Doe"
}
let query = makeQuery(data)
console.log(query)
一种更简单的方法:
Search.prototype.makeQuery = function (data) {
let result = {};
data.orderId && (result["order_id"] = data.orderId);
data.userMobileNumber && (result["user.Number"] = {$regex : data.userMobileNumber});
data.userFullName && (result["user.Name"] = {$regex: data.userFullName});
return result;
};
假设你有很多字段或想要修改它们,你可以创建一个地图。现在,你的代码工作,我的解决方案是矫枉过正,但它可能在将来有用:
const interestingData = new Map()
//I tried to imitate your values.
// Use whatever function you want here as a callback.
// value is the value you request, the callback must return the value you want to set.
interestingData.set("order_id", value => value)
interestingData.set("user.Number", value => ({ $regevalue: value }))
interestingData.set("user.Name", value => ({ $regevalue: value }))
//Tgis is a Factory in case you need several search.
const makeSearch = fields => data => {
let result = {}
fields.forEach((callBack, field) => {
if (data[field])
result[field] = callBack(data[field])
})
return result
}
//Creating a searching function
const myResearch = makeSearch(interestingData)
//Fake examples
const data1 = {
order_id: 'ertyui',
"user.Number": "ertyuio",
"user.Name": "ertyuio",
azerr: 123456
}
const data2 = {
order_id: 'ertyui',
"user.Number": "ertyuio",
}
console.log(myResearch(data1))
console.log(myResearch(data2))
它并不简单,但它更具可扩展性,当你有许多参数时,它会在大规模上更快。它也是可重复使用的。希望有所帮助!
不确定你是否认为这是代码优化,但你可以使用if
摆脱Object.assign
语句:
Search.prototype.makeQuery = function (data) {
return Object.assign({},
data.orderId && { order_id: data.orderId },
data.userMobileNumber && {
'user.Number': { $regex : data.userMobileNumber },
},
data.userFullName && {
'user.Name': { $regex : data.userFullName },
},
)
};
如果你可以使用更新的JS功能(使用转换器或其他方式),你可以使用Object rest/spread
来获得更简洁的语法:
Search.prototype.makeQuery = (data) => ({
...data.orderId && { order_id: data.orderId },
...data.userMobileNumber && {
'user.Number': { $regex : data.userMobileNumber },
},
...data.userFullName && {
user.Name': { $regex : data.userFullName },
},
});
编辑1:请注意,所有这些都是纯函数,没有任何突变发生