1

I have a payload that I'm trying to flatten but haven't had much luck. The payload looks like:

    {payload=
        [xyz,
        [{creation: 1501535135,
          id: reference_1},
         {creation: 225351535,
          id: reference_2 }]
        abc,
        [{creation: 129495124,
          id: reference_3},
         {creation: 151352244,
          id: reference_4 }]
        [{creation: 1501535135,
          id: reference_1},
         {creation: 225351535,
          id: reference }]
        [{creation: 1501535135,
          id: reference_3},
         {creation: 225351535,
          id: reference_4}]
        ]
     }

The output I would like in the end is a single array with all the creation and id values, so something like:

    {payload=
        [{creation: 1501535135,
          id: reference_1},
         {creation: 225351535,
          id: reference_2,
          {creation: 1501535135,
          id: reference_3},
         {creation: 225351535,
          id: reference_4}]
     }

I tried a few different things, firstly I tried to transform it by filtering only one object type of array so that the strings (xyz, abc) get dropped:

singleArray: payload filter ((element, index) -> typeOf(element) == "array")

At which point I was going to flatten the payload. However, the filter was throwing an error.

2
  • 1
    Please don't use this mechanism to compare types rather user typeOf(element) is Array Commented Nov 22, 2019 at 14:43
  • Thanks for the suggestion, I'm still getting the error "There is no variable named 'typeOf'" Commented Nov 22, 2019 at 14:50

2 Answers 2

2

Not sure why you are getting an error when using typeOf, but an alternative method is to use is as machaval had mentioned:

NOTE: below examples using DW 2.0 syntax

singleArray: payload filter ((element, index) -> (element is Array)

or this as a shorthand

singleArray: payload[?($ is Array)]

So to get your expected result I would use this:

singleArray: flatten(payload[?($ is Array)])

If you find you still need typeOf this should work (again not sure why you're seeing that error)

singleArray: payload filter ((element, index) -> (typeOf(element) as String) == "Array")

typeOf returns the Type type not the String type so it has to be converted to compare to a string

Sign up to request clarification or add additional context in comments.

Comments

0

Not sure why I was getting an error for utilizing typeOf but I switched to this notation which worked.

singleArray: flatten (payload filter $ is :array)

Thanks @short stack stevens for your response you helped nudge me in the right direction.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.