2016-09-29 72 views
0

我想知道爲什麼我的解決方案無法正常工作。我有以下幾點:如果測試多個三元運算符的語句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 *條件中不需要三元運算符! –

回答

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"); 
 
    } 
 
});

如果你想堅持與您開始使用,那麼你要使用一些括號更好的控制命令或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

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'); 

    } 
}); 
}); 
+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

很確定這是一個錯字,同時將原始源代碼縮減到可管理的大小。 –

+0

它是,更正,謝謝 – AnotherMike

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);

+0

這是一個非常有趣的方式來做到這一點,並使用功能性技術,我認爲這將工作,從來沒有想過要這樣做 – AnotherMike

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"}];