Properties of object into a comma separated string [closed]

With using Object.entries one needs to know and provide the to be processed reference … here metrics … exactly once. Thus the implementation of all the remaining code does not need to make assumptions about this reference’s name but just about this data-reference’s structure and how to reduce it …

const metrics = {
  "credit": {
    "sum": false,
    "mean": false,
    "max": true,
    "min": true,
  },
  "debit": {
    "sum": false,
    "mean": true,
    "max": true,
    "min": false,
  },
};

console.log(
  Object
    .entries(metrics)
    .reduce((list, [key, obj]) => [

      ...list,
      ...Object
        .entries(obj)
        .reduce((list, [value, bool]) =>

          (bool && [...list, `${ key }:${ value }`]) || list,
          []
        )
    ], []).join(',')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top