假设我们有三个不同的类别,购买,持有,出售
我们希望能够将“股票”对象数组传递到函数中,其中对象具有属性,例如
var stocks = [
{ticker: BACON, industry: food , isTech: true, isGreen: false, isIntl = true, is52weekHigh: true},
{ticker: PEE, industry: plumbing, isTech: true, isGreen: true, isIntl = true, is52weekHigh: false},
{ticker: BEER, industry: beverage, isTech: true, isGreen: true, isIntl = true, is52weekHigh: true},该函数应应用由用户设置的规则,将每个股票分类为三个类别之一,购买持有卖出。
规则很简单:这个和那个,这个或那个,这个但不是那个。
例如,购买:标准1:(isTech = true,isIntl = true,股票代码不等于小便)或标准2:(行业=饮料或行业=食品)
我真的不知道该如何处理它!任何帮助都是非常感谢的。
发布于 2014-04-26 23:04:59
如果你从一个受限的UI构建逻辑,那么使用eval应该是安全的。
我在Firefox的Scratchpad中运行了以下代码。
注意:-在每个对象中使用字符串而不是用于培根、食物等的变量,并需要将=替换为: after isIntl
var stocks = [
{ticker: 'BACON', industry: 'food' , isTech: true, isGreen: false, isIntl : true, is52weekHigh: true},
{ticker: 'PEE', industry: 'plumbing', isTech: true, isGreen: true, isIntl :true, is52weekHigh: false},
{ticker: 'BEER', industry: 'beverage', isTech: true, isGreen: true, isIntl : true, is52weekHigh: true}
]
for(var i=0;i<stocks.length;i++) {
stock=stocks[i];
console.log(eval((stock['isTech'] && stock['isIntl'] && stock['ticker'] != 'PEE') || (stock['industry']=='beverage' || stock['industry']=='food')));
}https://stackoverflow.com/questions/23311509
复制相似问题