使用一些評論,像這樣的東西可能會幫助你?
var data = {
count: 11,
length: 27,
days: 3
};
var fromServer = [{field: 'count', operator: '>', value: '10'}, {field: 'length', operator: '>', value: '3'}];
if (checkObjConditions(fromServer)) {
console.log("yes");
} else {
console.log("no");
}
function checkObjConditions(co) {
//var conditions = c.split("&&");
var isCondition = true;
for (var a = 0; isCondition && a < co.length; a++) {
//var c = conditions[a].trim().split(",");
var r = compare(co[a]['field'], co[a]['operator'], co[a]['value']);
console.log(">", r);
if (!r)
isCondition = false;
}
return isCondition;
}
function compare(a, operator, b) {
var ans = false;
switch (operator) {
case '<':
if (data[a] < parseInt(b))
ans = true;
break;
case '>':
console.log(data[a], parseInt(b))
if (data[a] > parseInt(b))
ans = true;
break;
// ... and other cases also
}
return ans;
}
是什麼刺痛可能具有價值? –
您遇到架構問題。你應該發送你的條件不是字符串,而是作爲對象,像這樣''[{field:'count',operator:'>',value:'10'},{field:'length',operator:'>'' ,值:'3'}]' – degr
@degr我可以做到,但如何檢查每個條件?以及如何檢查這樣的條件'(a> 3 &&(b <3 || z> 6))'? – MixerOID