내 솔루션이 작동하지 않는 이유가 궁금합니다. "계획"여러 삼항 연산자를 테스트하는 명령문
//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
}
});
아마도 당신은'report.filter '대신 또는 추가로? – apokryfos
'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
여기에 귀하의 질문이 무엇이든간에, 어쨌든이 코드 작업이 변경 될 수 있다면, 그것은 매우 읽을 수있는 형식으로 변경하는 것이 좋습니다. * if * 조건 안에 삼항 연산자가 필요하지 않습니다! –