paulroth3d / jupyter-ijavascript-utils

Utility library for working with iJavaScript - a Jupyter Kernel
1 stars 0 forks source link

Supporting Coalescing a set of object properties and merging into arrays #58

Open paulroth3d opened 10 months ago

paulroth3d commented 10 months ago

Feature request, but a convenience built on existing logic - only because it may happen more often that I'd think.

Say for example, you have a set of objects and you want to come up with one single object that removes duplicates

or another one that doesn't just use the first value, but instead creates an array of all the values that were used.

this can currently be accomplished through creating a new object and calling the utils.aggregate function, like:

//-- coalesce as it finds the first non-null value
utils.object.keys(list)
    .reduce(
        (result, key) => utils.object.assign(result, key, utils.agg.first(list, key)), {})
    );

or

//-- finds all unique values for those fields
utils.object.keys(list)
    .reduce(
        (result, key) => utils.object.assign(result, key, utils.agg.unique(list, key)), {})
    );

as effectively the same as the following, only they explicitly specify the fields, and how to handle them.

({
    first: utils.agg.first(list, 'first'),
    last: utils.agg.first(list, 'last'),
    ...
})

and

({
    first: utils.agg.unique(list, 'first'),
    last: utils.agg.unique(list, 'last'),
    ...
})

where utils.agg.first lists through the objects and finds the first non-null value in the property provided. Alternatively, the utils.agg.unique provides all the unique values used. But you need to specify the list of all the fields to provide.