0
以我的node.js模塊的更智能的方式,我在陣列的一些數據,我需要構造查詢:構建查詢
var valueClause = "(fieldA = '" + data.fieldA + "' AND fieldB = '";
var whereClause = ' WHERE ';
var hasAdded = false;
data.accounts.forEach(function (account) {
whereClause += valueClause + account.fieldB + "') OR ";
hasAdded = true;
})
if (hasAdded) {
// remove OR
whereClause = whereClause.substring(0, whereClause.length - 3);
// use the whereClause in the query
...
}
在上面的代碼的結束時,如果我有2個賬戶我有whereClause:
'WHERE(FIELDA = 'ABCDE' AND fieldB = '0003')或(FIELDA = 'ABCDE' AND fieldB = '技術')或
我總是要刪除最後一個'OR'位。
有沒有更智能的方法來構建上述?