node.js - how to perform ternary operation in Nodejs -
am new nodejs.am trying perform ternary operation in nodejs code here code segment using
if (filter.serverid != "0") {// filter server details report filterjson(report, report[items].server, filter.serverid, function ( filterddata) { report = filterddata; }); }else report = report; can write code this
report = filter.serverid != "0" ? filterjson(report, report[items].server, filter.serverid, function (filterddata) { report = filterddata; }); : report
in short, have no reason use ternary operator. full explanation , solutions below.
the 2 code snippets saying different things. first 1 has two assignments. report = filterddata in anonymous function, if condition true, otherwise assigns report report.
the second code snippet has three assignments. report = filterddata in anonymous function condition true, , assigns result of filterjson report (i guessing filterjson returns undefined). if condition false, reassigns report report again.
solutions
so first of all, "else" nothing. assigns report itself. rid of it. secondly, assigning result filterjson report, that's not how want use filterjson (i assume).
this want.
if (filter.serverid != "0") { filterjson(report, report[items].server, filter.serverid, function (filterddata) { report = filterddata; }); } if want rid of curly braces, can that, considered style keep them.
if (filter.serverid != "0") filterjson(report, report[items].server, filter.serverid, function (filterddata) { report = filterddata; }); the ternary operator isn't usable here.
Comments
Post a Comment