2016-09-29 3 views
0

내 솔루션이 작동하지 않는 이유가 궁금합니다. "계획"여러 삼항 연산자를 테스트하는 명령문

//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 경우 항상 뭔가를하거나 다른 세 유형 중 하나 인 경우,하지만 경우에만 : 나는 다음과 같은이 isInReport 객체에서 true 그래서 내 예제에서 if 문은 item.type이 "Plan"또는 "Factual"이면 통과해야합니다.

왜이 코드가 작동하지 않습니까? 조금 이상한 논리라도 나에게 맞는 것 같다. 테스트를 마친 후에도 항상 모든 유형이 반환됩니다. 어떤 도움을 주셔서 감사합니다!

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"'yelding true, [진술이 이미 진실이므로] (https://developer.mozilla.org/en/docs)이 더 이상 필요하지 않음을 확인할 수 있습니까?/Glossary/Truthy) – Bonatti

+0

여기에 귀하의 질문이 무엇이든간에, 어쨌든이 코드 작업이 변경 될 수 있다면, 그것은 매우 읽을 수있는 형식으로 변경하는 것이 좋습니다. * if * 조건 안에 삼항 연산자가 필요하지 않습니다! –

답변

1

은 당신이 원하는 않았다. 신고의 4 개 항목에 대한 결과로 기대되는 것을 확인할 수 있습니까?

당신은 당신이 더 나은 순서 또는 opperations을 제어하기 위해 일부 브래킷을 사용하려면 시작하는 방식을 고수하고 싶었다면

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

. I는 허용 된 값의 어레이를 생성하려는

+0

질문에서 소리가 나지 않습니다. 아니요. 'isInReport'의 각 타입은'item.type'에서 허용 된 두 개의 서로 다른 문자열 값과 관련이 있습니다. –

+0

@DaveNewton 어떤 유스 케이스가 실패합니까? – JonSG

+0

감사합니다, 이것이 작동하는 것처럼 보입니다. "계획"이 항상 통과하고 사실적 인 것이 isInReport 객체에서 "사실"이기 때문에 "계획"과 "사실적"에 대해서만 전달해야합니다. – AnotherMike

0

I 오류가 표시되지 않습니다 ... 난 여기까지 바이올린 : 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

네, 그게 내가 얻는 것입니다. 항목에 대해서만 경고해야합니다.type === "Plan"및 "Factual"이지만 4 가지 유형 모두에 대해 경고합니다. – AnotherMike

+0

Humm IF 문을 다음과 같이 정리해야 할 것 같습니다 : if ( (item.type === "계획") || (item.type === (isInReport.factual)? "Factual" : "플랜")) || (item.type === (isInReport.eac)? "EAC": "계획")) || (item.type === ((isInReport.variance)? " Variance ":"Plan ")) ) { http://jsfiddle.net/6mp0b1wm/ –

0

을 당신은 당신의 '보고서'배열의 요소 사이에 쉼표가 누락되었습니다. 이 제안 의견이 있습니다

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

가 정확하지 :

+0

원본 소스 코드를 관리 가능한 크기로 축소하는 동안 오타가 발생했는지를 확실히 확인하십시오. –

+0

감사합니다. – AnotherMike

0

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

후 필터를 사용한다. 이렇게하면 다중 중첩 된 if/3 중 혼합보다 훨씬 쉽게 읽고 유지할 수 있습니다.

+0

이것은 이것을하기에 정말로 흥미로운 방법이며, 기능적 기술을 사용하면 이것이 가능할 것이라고 생각하지 않습니다. – AnotherMike

0

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);
당신은 예상 된 결과

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"}];에 쉼표를 잊었)

관련 문제