如果测试多个三元运算符的语句Javascript

问题描述:

我想知道为什么我的解决方案无法正常工作。我有以下几点:如果测试多个三元运算符的语句Javascript

//tells if type should be included in the row data 
isInReport = {factual: true, eac: false, variance: false} 

//report has hundreds of objects, each with a type of either Plan, Factual, EAC, Variance 
report = [{type: "Plan"}, {type: "Factual"}, {type: "EAC"}, {type: "Variance"}]; 

我需要遍历数组报告,并做一些事情总是如果项目。形式是“规划纲要”,或者如果是其他3种类型之一,但只有当它是在isInReport对象中为true。所以在我的例子中,如果item.type是“Plan”或“Factual”,if语句应该通过

为什么不能使用此代码?即使有点奇怪,逻辑对我来说似乎是正确的。当我测试它时,无论如何都会返回所有类型。谢谢你的帮助!

report.map(function (item) { 
    if (
    item.type === "Plan" || 
    item.type === (isInReport.factual) ? "Factual" : "Plan" || 
    item.type === (isInReport.eac) ? "EAC" : "Plan" || 
    item.type === (isInReport.variance) ? "Variance" : "Plan" 
) { 
    //do stuff 
    } 
}); 
+0

也许你需要'report.filter '而不是另外? – apokryfos

+0

如果行'report = [{type:“Plan”} {type:“Factual”} {type:“EAC”} {type:“Variance”}];'是正确的,你能重新检查一下吗?似乎缺少了许多逗号,例如:'report = [{type:“Plan”},{type:“Factual”},{type:“EAC”},{type:“Variance”}];'另外,你是否可以确认'item.type ===“Plan”'是否正确,[不需要更多的检查,因为语句已经是真实的](https://developer.mozilla.org/en/docs/Glossary/Truthy) – Bonatti

+0

不知道你的问题在哪里,无论如何,即使这个代码工作,如果你可以改变它,它应该是一个很好的可读性更好的形式。 * if *条件中不需要三元运算符! –

难道你想做的事:

if (item.type === "Plan" || isInReport[ item.type.toLowerCase() ]) { 
    //do stuff 
} 

有评论暗示这是不正确的。您是否可以确认您对报告中4项产品的期望?

//tells if type should be included in the row data 
 
isInReport = {factual: true, eac: false, variance: false} 
 

 
//report has hundreds of objects, each with a type of either Plan, Factual, EAC, Variance 
 
report = [{type: "Plan"}, {type: "Factual"}, {type: "EAC"}, {type: "Variance"}]; 
 

 
report.forEach(function(item){ 
 
    if (item.type === "Plan" || isInReport[ item.type.toLowerCase() ]) { 
 
    console.log("Item Type:" + item.type + " PASSED TEST"); 
 
    } else { 
 
    console.log("Item Type:" + item.type + " FAILED TEST"); 
 
    } 
 
});

如果你想坚持与您开始使用,那么你要使用一些括号更好的控制命令或opp​​erations的方式。

//tells if type should be included in the row data 
 
isInReport = {factual: true, eac: false, variance: false} 
 

 
//report has hundreds of objects, each with a type of either Plan, Factual, EAC, Variance 
 
report = [{type: "Plan"}, {type: "Factual"}, {type: "EAC"}, {type: "Variance"}]; 
 

 
report.forEach(function(item){ 
 
    if (
 
    item.type === "Plan" || 
 
    item.type === (isInReport.factual ? "Factual" : "Plan") || 
 
    item.type === (isInReport.eac ? "EAC" : "Plan") || 
 
    item.type === (isInReport.variance ? "Variance" : "Plan") 
 
) { 
 
    console.log("Item Type:" + item.type + " PASSED TEST"); 
 
    } else { 
 
    console.log("Item Type:" + item.type + " FAILED TEST"); 
 
    } 
 
});

+0

听起来不像它的问题,没有; 'isInReport'中的每个类型都与'item.type'中允许的两个不同的字符串值相关联。 –

+0

@DaveNewton什么用例会失败? – JonSG

+0

谢谢,这似乎工作,它应该只通过“计划”和“事实”,因为“计划”总是通过和isInReport对象中的事实是“真实的” – AnotherMike

我没有看到错误......我拨弄它在这里:http://jsfiddle.net/Lnkky0fw/

$(document).ready(function() { 
var isInReport = {factual: true, eac: false, variance: false}; 

//report has hundreds of objects, each with a type of either Plan, Factual, EAC, Variance 
var report = [{type: "Plan"},{type: "Factual"},{type: "EAC"},{type: "Variance"}]; 

report.map(function (item) { 
    if (
    item.type === "Plan" || 
    item.type === (isInReport.factual) ? "Factual" : "Plan" || 
    item.type === (isInReport.eac) ? "EAC" : "Plan" || 
    item.type === (isInReport.variance) ? "Variance" : "Plan" 
) { 
    //do stuff 
    alert('ok'); 

    } 
}); 
}); 
+0

是的,这就是我得到的。它只应该提醒项目。键入===“计划”和“事实”,但它提醒所有4种类型。 – AnotherMike

+0

Humm好像你需要组织你的IF语句为: if( (item.type ===“Plan”)|| (item.type ===((isInReport.factual)?“Factual” :“Plan”))|| (item.type ===((isInReport.eac)?“EAC”:“Plan”))|| (item.type ===((isInReport.variance)?“ Variance“:”Plan“)) ){ http://jsfiddle.net/6mp0b1wm/ –

你在你的“报告”数组元素之间缺少逗号。

+0

很确定这是一个错字,同时将原始源代码缩减到可管理的大小。 –

+0

它是,更正,谢谢 – AnotherMike

我想创建允许的值的阵列,然后使用滤波器。这会比多嵌套if/ternary混合物更容易阅读和维护。

var isInReport = { 
 
    factual: true, 
 
    eac: false, 
 
    variance: false 
 
}; 
 

 
var report = [{ type: "Plan" }, { type: "Factual" }, { type: "EAC" }, { type: "Variance" }]; 
 

 
var allowed = ["plan"] 
 
    .concat(Object.keys(isInReport) 
 
    .map(function (key) { 
 
     if (isInReport[key]) return key.toLowerCase(); 
 
    }).filter(function (v) { 
 
     return v; 
 
    }) 
 
); 
 

 
var filtered = report.filter(function (d) { 
 
    if (allowed.indexOf(d.type.toLowerCase()) > -1) return true; 
 
    return false; 
 
}); 
 

 
console.log(filtered);

+0

这是一个非常有趣的方式来做到这一点,并使用功能性技术,我认为这将工作,从来没有想过要这样做 – AnotherMike

你需要用括号括起来三元表达式来获得预期的结果

if (
    item.type === "Plan" || 
    item.type === ((isInReport.factual) ? "Factual" : "Plan") || 
    item.type === ((isInReport.eac) ? "EAC" : "Plan") || 
    item.type === ((isInReport.variance) ? "Variance" : "Plan") 
) 

(和你忘了逗号

report = [{type: "Plan"},{type: "Factual"},{type: "EAC"},{type: "Variance"}];