0

I have a arry called a tableFilterFields like this:

 const tableFilterFields = [
    {
      label: "اfrom",
      name: "StartDate",
      type: FilterControls.DatePicker,
      defaultValue: new Date(new Date().setDate(new Date().getDate() - 3100)),
    }
    props.id === undefined
      ? {
          label: "IdentityTitle",
          name: "IdentityTitle",
          type: FilterControls.Input,
        },
        {
          label: "AccountCode",
          name: "AccountCode",
          type: FilterControls.Input,
        },
         {
          label: "InstrumentPersianCode",
          name: "InstrumentPersianCode",
          type: FilterControls.Input,
        },
        {
      label: "TradeSideTitle ",
      name: "TradeSideTitle",
      type: FilterControls.Input,
        }
        
      : 


  {
            label: "InstrumentPersianCode",
            name: "InstrumentPersianCode",
            type: FilterControls.Input,
          },
          {
        label: "TradeSideTitle",
        name: "TradeSideTitle",
        type: FilterControls.Input,
          },
      ];

I want to apply a condition that If prop was not undefined ....How should I do this?

1
  • When I apply the condition in this way, I get a syntax error Commented Jul 6, 2021 at 12:24

3 Answers 3

1

It would be better to do the conditional operation outside the array definition. You can achieve this by conditionally doing push operation

if(props.id === undefined){
  tableFilterFields.push({
          label: "IdentityTitle",
          name: "IdentityTitle",
          type: FilterControls.Input,
        },
        {
          label: "AccountCode",
          name: "AccountCode",
          type: FilterControls.Input,
        },
         {
          label: "InstrumentPersianCode",
          name: "InstrumentPersianCode",
          type: FilterControls.Input,
        },
        {
      label: "TradeSideTitle",
      name: "TradeSideTitle",
      type: FilterControls.Input,
        })
} else {
     tableFilterFileds.push(  {
            label: "InstrumentPersianCode",
            name: "InstrumentPersianCode",
            type: FilterControls.Input,
          },
          {
        label: "TradeSideTitle ",
        name: "TradeSideTitle",
        type: FilterControls.Input,
          })
}
Sign up to request clarification or add additional context in comments.

Comments

0
import React from "react";

const index = ({ array }) => {
  if (array === undefined) return <>Array not found</>;

  return (
    <div>
      {array.map((x) => (
        <RenderArray key={x.id} {...x} />
      ))}
    </div>
  );
};

export default index;

you can do something like this for condition randering

Comments

0

First insert the common elements, then push the rest conditionally:

const arr = ["common"];
const condition = undefined === void 0;
arr.push.apply(arr, condition ? ["variant1", 2, 3] : ["variant2", 4, 5]);

console.log(arr);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.