Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
How to use ternary operator for multiple conditions?

trying to add multiple conditions using ternary operator , values are coming false every time what is the issue with below code ? based on isActive flag value should set

index.js

   const _res: any = {}

_res.memberPreferences.channels.EM = (details.memberPreferences.channels[0].channelType === "EM" && details.memberPreferences.channels[0].isActive === "true") ? true : false;
_res.memberPreferences.channels.SMS = (details.memberPreferences.channels[0].channelType === "SMS" && details.memberPreferences.channels[0].isActive === "true") ? true : false

data

here is how the data is returned

{
    "details": {
        "memberPreferences": {
            "channels": [{
                    "channelType": "EM",
                    "isActive": "true"
                },
                {
                    "channelType": "SMS",
                    "isActive": "false"
                }
            ]
        }
    }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need no ternary, just the result of the comparison.

const
    details = { memberPreferences: { channels: [{ channelType: "EM", isActive: "true" }, { channelType: "SMS", isActive: "false" }] } },
    _res = { memberPreferences: { channels: {} } },
    target = _res.memberPreferences.channels;

details.memberPreferences.channels.forEach(({ channelType, isActive }) => {
    target[channelType] = isActive === "true";
});

console.log(_res);

about 4 years ago · Juan Pablo Isaza Report

0

You don't actually need ternary operator here. The condition itself returns either true or false. Also, it's good to create separate variables for details.memberPreferences.channels[0] and channel_zero.isActive == "true" condition. And here there is no need to use === over ==. So the index.js should look like this:

const _res: any = {};
const channel_zero: any = details.memberPreferences.channels[0];
const is_active: boolean = channel_zero.isActive == "true";

_res.memberPreferences.channels.EM = (channel_zero.channelType == "EM" && is_active);
_res.memberPreferences.channels.SMS = (channel_zero.channelType == "SMS" && is_active) ;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!