1

We are using https://pbeshai.github.io/tidy/ for some data manipulation in javascript. We are looking to concatenate all strings for a grouped field in a summarise() after using groupby(). Trying to do something like:

let myArr = [
  { group: 1, field: 'okay' },
  { group: 1, field: 'sir' },
  { group: 2, field: 'yes' },
  { group: 2, field: 'there' },
]

tidy(myArr,
  groupBy('group', [
    summarise({
      allStrings: concat('field', sep=' ')
    })
  ])
)

and get as an output:

[
  { group: 1, allStrings: 'okay sir' },
  { group: 2, allStrings: 'yes there' }
]

Not seeing anything in the docs in the summarizers section for this unfortunately. allStrings: concat('field', sep=' ') is invalid as there is no concat summarizer function in tidy.js... Is this possible in tidy.js? If not, is there a straightforward way to string_agg / concat strings within a group in javascript like this?

Canovice
  • 9,012
  • 22
  • 93
  • 211

2 Answers2

1

There are so many group array of objects by key having items either pushed into array/ concatenated/ summed/ counted/ etc examples on this site. But maybe someone will find this useful.

let myArr = [
  { group: 1, field: 'okay' },
  { group: 1, field: 'sir' },
  { group: 2, field: 'yes' },
  { group: 2, field: 'there' },
]

let obj = myArr.reduce(function(agg, item) {
  
  // do your group by logic below this line
  
  agg[item.group] = agg[item.group] || [];
  agg[item.group].push (item.field)
  
  // do your group by logic above this line
  
  return agg
}, {});

// this one also useful to convert to array
let result = Object.entries(obj).map(function ([key, value]) {
  return {
    group: key,
    allStrings: value.join(" ")
  }
})


console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
IT goldman
  • 14,885
  • 2
  • 14
  • 28
1

You're right – there is not yet a concat summarizer in tidyjs, so you need to do it manually. Here's an example of how:

const myArr = [
  { group: 1, field: 'okay' },
  { group: 1, field: 'sir' },
  { group: 2, field: 'yes' },
  { group: 2, field: 'there' },
];

const output = tidy(
  myArr,
  groupBy('group', [
    summarize({
      allStrings: (items) => items.map((d) => d.field).join(' '),
    }),
  ])
);

This will produce the following output:

[
  {"group": 1, "allStrings": "okay sir"},
  {"group": 2, "allStrings": "yes there"}
]

Essentially, you write a custom summarizer that maps each item into just the string value you care about, then you join those with a ' ' to get your final concatenated string.