Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$map does not return an array for input arrays of length 1 #708

Open
vecerek opened this issue Aug 20, 2024 · 4 comments
Open

$map does not return an array for input arrays of length 1 #708

vecerek opened this issue Aug 20, 2024 · 4 comments
Labels

Comments

@vecerek
Copy link

vecerek commented Aug 20, 2024

According to the docs, $map should return an array. However, if the input data is an array containing exactly one element, it returns the value returned by the function passed to $map as the second argument.

Reproduction: https://try.jsonata.org/hW13DjVzw

In case the link stops working at some point in the future, here is the inlined version of the reproduction steps:

Data

[{ "a": 1 }]

Jsonata

$map($, function($el) {
  { "a": $el.a + 1 }
})

Expected output

[{ "a": 2 }]

Actual output

{ "a": 2 }
@vecerek vecerek changed the title $map does not return an array on input arrays of length 1 $map does not return an array for input arrays of length 1 Aug 20, 2024
@vecerek
Copy link
Author

vecerek commented Aug 20, 2024

Temporary workaround for people struggling with this in production:

(
  $correctMap := function($data, $callback) {(
    $res := $map($data, $callback);

    $type($res) = "array" ? $res : [$res]
  )};

  $correctMap($, function($el) {
    { "a": $el.a + 1 }
  })
)

Playground link: https://try.jsonata.org/sa2yM0nj8

@vecerek
Copy link
Author

vecerek commented Aug 21, 2024

Another thing I noticed, if the input array is empty, the result is ** no match ** instead of an empty array. Are my expectations simply wrong here? The way I interpret:

Returns an array containing the results of applying the function parameter to each value in the array parameter.

from the docs is that the function returns an array in all cases, not just sometimes.

Playground link: https://try.jsonata.org/pkveSyNDj

@elviejo79
Copy link

I think your intuitions are correct.
$map should return an array in all case:

empty array -> empty array
1 element array -> 1 element array
n element array -> n element array

@netomi
Copy link
Contributor

netomi commented Sep 23, 2024

I also stumbled upon this weird behavior. Adding an array constructor at the end like $map([1], ...)[] solves it however when using the map together with a chain operator it currently does not work even with the workaround.

I created a PR to fix that also in this case to be able to use the workaround: #714

However I would like to note that the current behavior of map is not what you would expect and thus it can not really be used. The workaround for me is to use a lambda function that iterates over the elements of an array instead of using the built-in function map which should do the same.

If this behavior is wanted, you could add an additional built-in function to return a singleton if explicitly requested, but not do that be default imho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants