使用一些评论,像这样的东西可能会帮助你?
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