AnotherMike
AnotherMike

Reputation: 2913

If Statement that Tests Multiple Ternary Operators Javascript

I'm wondering why my solution doesn't work. I have the following:

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

I need to loop through the report array, and do something always if item.type is "Plan," or if it is one of the other 3 types, but only if it is true in the isInReport object. So in my example the if statement should pass if item.type is "Plan," or "Factual"

Why doesn't this code work? The logic seems right to me even if a little odd. When I've tested it always returns all types no matter what. Thanks for any help!

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

Upvotes: 0

Views: 59

Answers (5)

JonSG
JonSG

Reputation: 13087

Did you want to do:

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

There is a comment suggesting this is not correct. Can you confirm what you expect as a result for the 4 items in report?

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

If you wanted to stick with the way you started with then you want to use some brackets to better control the order or 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" ||
    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");
  }  
});

Upvotes: 1

user2314737
user2314737

Reputation: 29317

You need to enclose the ternary expressions in parentheses to obtain the expected result

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

(and you forgot the commas in

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

Upvotes: 0

baao
baao

Reputation: 73241

I'd create an array of the allowed values, then use filter. That will make it much easier to read and maintain than a multiple nested if/ternary mixture.

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

Upvotes: 0

Steve Thorpe
Steve Thorpe

Reputation: 91

You are missing commas between elements in your 'report' array.

Upvotes: 0

JC Hernández
JC Hernández

Reputation: 807

I don't see the error... i fiddle it up here: 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');

  }
});
});

Upvotes: 0

Related Questions