I have a arry called a tableFilterFields like this:
JavaScript
x
44
44
1
const tableFilterFields = [
2
{
3
label: "اfrom",
4
name: "StartDate",
5
type: FilterControls.DatePicker,
6
defaultValue: new Date(new Date().setDate(new Date().getDate() - 3100)),
7
}
8
props.id === undefined
9
? {
10
label: "عنوان شخص",
11
name: "IdentityTitle",
12
type: FilterControls.Input,
13
},
14
{
15
label: "کد بورسی",
16
name: "AccountCode",
17
type: FilterControls.Input,
18
},
19
{
20
label: "نماد ",
21
name: "InstrumentPersianCode",
22
type: FilterControls.Input,
23
},
24
{
25
label: "نوع معامله ",
26
name: "TradeSideTitle",
27
type: FilterControls.Input,
28
}
29
30
:
31
32
33
{
34
label: "نماد ",
35
name: "InstrumentPersianCode",
36
type: FilterControls.Input,
37
},
38
{
39
label: "نوع معامله ",
40
name: "TradeSideTitle",
41
type: FilterControls.Input,
42
},
43
];
44
I want to apply a condition that If prop was not undefined ….How should I do this?
Advertisement
Answer
It would be better to do the conditional operation outside the array definition. You can achieve this by conditionally doing push operation
JavaScript
1
34
34
1
if(props.id === undefined){
2
tableFilterFields.push({
3
label: "عنوان شخص",
4
name: "IdentityTitle",
5
type: FilterControls.Input,
6
},
7
{
8
label: "کد بورسی",
9
name: "AccountCode",
10
type: FilterControls.Input,
11
},
12
{
13
label: "نماد ",
14
name: "InstrumentPersianCode",
15
type: FilterControls.Input,
16
},
17
{
18
label: "نوع معامله ",
19
name: "TradeSideTitle",
20
type: FilterControls.Input,
21
})
22
} else {
23
tableFilterFileds.push( {
24
label: "نماد ",
25
name: "InstrumentPersianCode",
26
type: FilterControls.Input,
27
},
28
{
29
label: "نوع معامله ",
30
name: "TradeSideTitle",
31
type: FilterControls.Input,
32
})
33
}
34