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

Add functions to map an array into an object #1157

Closed
bruceg opened this issue Dec 2, 2024 · 3 comments
Closed

Add functions to map an array into an object #1157

bruceg opened this issue Dec 2, 2024 · 3 comments
Labels
type: feature A value-adding code addition that introduce new functionality. vrl: stdlib Changes to the standard library

Comments

@bruceg
Copy link
Member

bruceg commented Dec 2, 2024

When manipulating data that has been parsed from CSV or other columnar data sources, it is useful to be able to map those columns into an object with named fields. I propose adding two functions to accomplish this, mirroring the approach in Python: object and zip:

  • zip takes a number of arrays as input and produces a single array as output containing a tuple of elements from each array. For example zip([1,2,3],[4,5,6],[7,8,9]) returns [[1,4,7],[2,5,8],[3,6,9]].
  • object takes takes a zipped array as above with (key, value) pairs and produces an object as output. For example object([["foo":1],["bar":2]]) returns {"foo":1,"bar":2}.
@bruceg bruceg added vrl: stdlib Changes to the standard library type: feature A value-adding code addition that introduce new functionality. labels Dec 2, 2024
@jszwedko
Copy link
Member

jszwedko commented Dec 2, 2024

I'd suggest a different name for the function that takes an array of "tuples" than object, which already exists. Maybe from_pairs? That's what lodash calls it. Maybe there are other examples from other languages or libraries we could use for inspiration.

@bruceg
Copy link
Member Author

bruceg commented Dec 2, 2024

I missed that object was already a function, and I'm on board with something like from_pairs, but isn't that a little ambiguous as to what the output of such a function would be?

@jszwedko
Copy link
Member

jszwedko commented Dec 2, 2024

True, could call it object_from_pairs to be explicit. Granted we have other functions already where the return type isn't clear just from the name itself (e.g. from_unix_timestamp).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature A value-adding code addition that introduce new functionality. vrl: stdlib Changes to the standard library
Projects
None yet
Development

No branches or pull requests

2 participants