cancel
Showing results for 
Search instead for 
Did you mean: 

IF Statement in Expressions Library file

adam_g
New Contributor II
Hi,

I have an expressions library file which creates a JSON object like this:

 

...
.map((elem, idx, arr) =>
    {
        "fieldA": elem.fieldA,
        "fieldB": [
            {
                "fieldC1": elem.fieldC1,
                "fieldC2": elem.fieldC2
            },
            {
                "fieldD1": elem.fieldD1,
                "fieldD2": elem.fieldD2
            },
            {
                "fieldE1": elem.fieldE1,
                "fieldE2": elem.fieldE2
            }
        ]
    }
)
...

 

"fieldB" is an array of objects. The first object ("fieldC") will always be present, but for the second ("fieldD") and third ("fieldE") objects that is not always the case.
How can I create a condition that will remove the whole {...} based on whether elem.fieldD1 or elem.fieldE1 exist?
 
I think there is only the ternary operator ( a ? b : c ), but I am not sure how to insert the ternary operator here.
Is something like this correct:

 

...
{
    "fieldC1": elem.fieldC1,
    "fieldC2": elem.fieldC2
}, /* what about this "," (comma)? If D1 does not exist, then the comma should not be there.*/
(elem.fieldD1) ? "{ "fieldD1": elem.fieldD1, "fieldD2": elem.fieldD2 }," : ""
(elem.fieldE1) ? "{ "fieldE1": elem.fieldE1, "fieldE2": elem.fieldE2 }" : ""
...

 

Lastly, you only have these 3 objects inside the array and nothing more.
The order is always kept. If fieldD does not exist, fieldE doesn't either.
 
Does someone know how I can achieve this result?
 
Best regards,
Adam
1 ACCEPTED SOLUTION

koryknick
Employee
Employee

@adam_g - you were very close - try this

 

.map((elem, idx, arr) =>
    {
        "fieldA": elem.fieldA,
        "fieldB": [
            {
                "fieldC1": elem.fieldC1,
                "fieldC2": elem.fieldC2
            }
        ]
        .concat(
	       elem.get('fieldD1') > ''
	       ? [
   	            {
                    "fieldD1": elem.fieldD1,
                    "fieldD2": elem.fieldD2
                }
	        ] 
	       : []
        )
        .concat(
           elem.get('fieldE1') > ''
	       ? [
                {
                    "fieldE1": elem.fieldE1,
                    "fieldE2": elem.fieldE2
                }
             ] 
	        : []
    )
    }
)

 

Hope this helps!

View solution in original post

3 REPLIES 3

koryknick
Employee
Employee

@adam_g - you were very close - try this

 

.map((elem, idx, arr) =>
    {
        "fieldA": elem.fieldA,
        "fieldB": [
            {
                "fieldC1": elem.fieldC1,
                "fieldC2": elem.fieldC2
            }
        ]
        .concat(
	       elem.get('fieldD1') > ''
	       ? [
   	            {
                    "fieldD1": elem.fieldD1,
                    "fieldD2": elem.fieldD2
                }
	        ] 
	       : []
        )
        .concat(
           elem.get('fieldE1') > ''
	       ? [
                {
                    "fieldE1": elem.fieldE1,
                    "fieldE2": elem.fieldE2
                }
             ] 
	        : []
    )
    }
)

 

Hope this helps!

adam_g
New Contributor II

Hi @koryknick,

Thank you again for replying!

Is there a way to check whether the value of elem.fieldD1 is null? The field elem.fieldD1 itself always exists, but the value is null sometimes. My bad for explaining it incorrectly.

I tried: elem.fieldD1 != null ? ...

But it did not work.

I updated my original reply to use Object.get() method; please test that result.

Hope this helps!