我想知道为什么我的解决方案不起作用。我有以下内容:
//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"}];
我需要循环遍历报表数组,并且如果item.type是“Plan”,或者它是其他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 :(得分:1)
你想做什么:
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");
}
});
如果您想坚持自己开始的方式,那么您希望使用一些括号来更好地控制顺序或操作。
//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");
}
});
答案 1 :(得分:0)
我没有看到错误...我在这里搞砸了: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');
}
});
});
答案 2 :(得分:0)
您的“报告”中的元素之间缺少逗号。阵列。
答案 3 :(得分:0)
我创建一个允许值的数组,然后使用过滤器。这将比多个嵌套的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);

答案 4 :(得分:0)
您需要将三元表达式括在括号中以获得预期结果
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"}];
)