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

82
Views
If-else inside array of objects using react

I have an array of objects where I define multiple icons that needs to be displayed.

const iconButtonList = [
        {
            icon: <AddIcon />,
            onClick: onClickOpenRuleEditor,
            isDisabled: false,
            label:"Add"
        },
        {
            icon: <EditIcon />,
            onClick: onClickOpenRuleEditor,
            isDisabled: isEditDisabled
        },
        {
            icon: <CopyIcon />,
            onClick: onClickCopyRules,
            isDisabled: true
        },
        {
            icon: <TrashIcon />,
            onClick: onDelete,
            isDisabled: isRemoveDisabled
        },
        {
            icon: <RefreshIcon />,
            onClick: onClickRefreshRulesGrid,
            isDisabled: false
        }];

I loop through through the array and display all the icons within a div element.

const gridActionButtons =
        <div>
            {
                iconButtonList.map((element, index) => (
                  <IconButton
                    key={index}
                    flat
                    secondary
                    label={element.label}
                    icon={element.icon}
                    isDisabled={element.isDisabled}
                    onClick={element.onClick}/>)
                )
            }
</div>;

In addition to the icons defined, I would like to display few other icons based on the tab selected, for which I would like to add if-else within the array. I tried to do something like this:

{
            icon: <RefreshIcon />,
            onClick: onClickRefreshRulesGrid,
            isDisabled: false
        },
        {
            if: {
                selectedTab:0
            },then :[{
                    icon: `<DeploymentIcon />`,
                    onClick: onClickDeploy,
                    isDisabled: true
                }],
                else:[{
                    
                        icon: `<DocumentExcelIcon />`,
                        onClick: onClickUndeploy,
                        isDisabled: true
                }]
            }
    ];

But this didn't work. Is it possible to achieve something like this without defining entire set of rest of the icons within if-else condition ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do something like this:

  {
    icon: <RefreshIcon />,
    onClick: onClickRefreshRulesGrid,
    isDisabled: false
  },
  selectedTab === 0 ?
  {
     icon: `<DeploymentIcon />`,
     onClick: onClickDeploy,
     isDisabled: true
  },
   :{
     icon: `<DocumentExcelIcon />`,
     onClick: onClickUndeploy,
     isDisabled: true
  }
];
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!