Forum Discussion

tarena's avatar
tarena
New Contributor III
7 months ago
Solved

mapValues returns null when adding certain condition

I'm using the following expression to parse date fields in an object. It works correctly. However, when I replace key.contains('date') with another string to compare, the expression returns null for the object.

$attributes.mapValues((value,key) => key.contains('date') ? Date.parse(value).toLocaleDateTimeString({"timeZone":"US/Pacific", "format":"yyyy-MM-dd HH:mm:ss"}) : value)

The behavior is constant with many, but not all strings. 'Item' works, but 'it' doesn't work. No single letter works except for 'z'.

What is this behavior? How can I filter for other keys?

 

  • tarena's avatar
    tarena
    7 months ago

    Thank you. This was pretty close to the problem. If there is an error in a function called in mapValues, a null object is returned as the result of map values. My original error was from adding an or condition that would include keys which couldn't be parsed as dates. This error turned the entire object to null, which was what I observed. 

3 Replies

  • tarena - Can you please provide some sample input data where this isn't working for you?  And show the results you are seeing that you believe to be incorrect?

  • alchemiz's avatar
    alchemiz
    Contributor III

    Hi tarena ,

    This might because Date.parse returns a not-date causing for it to set as null

    Now if you can check first the output from the Date.parse before setting up the value

    $attributes.mapValues((value,key) => key.contains('date') ? (Date.parse(value) instanceof Date ? Date.parse(value).toLocaleDateTimeString({"timeZone":"US/Pacific", "format":"yyyy-MM-dd HH:mm:ss"}) : value) : (value) )

     



    • tarena's avatar
      tarena
      New Contributor III

      Thank you. This was pretty close to the problem. If there is an error in a function called in mapValues, a null object is returned as the result of map values. My original error was from adding an or condition that would include keys which couldn't be parsed as dates. This error turned the entire object to null, which was what I observed.