I am seeking a method to tally substrings of object values. In other words, instead of the entire object containing a string, I want it where one key equals a string. An effective Xpath in XSLT is:
count(//v[contains(.,current-grouping-key())])
However, I am struggling to achieve this using JavaScript.
I have attempted the following:
const obj =
[ { v: 'Bla Blu Bli' },
{ v: 'Bla Blu Bli' },
{ v: 'Bla Blu' },
{ v: 'Bla Bli' }
];
const count = obj.reduce( function(sums,entry) {
sums[entry.v] = (sums[entry.v] || 0) + 1;
return sums;
},{});
console.log(count)
Unfortunately, this approach only counts exact strings. The output I receive is:
"Bla Blu Bli": 2,
"Bla Blu": 1,
"Bla Bli": 1
instead of
"Bla Blu Bli": 2,
"Bla Blu": 3,
"Bla Bli": 3
Is there a way to count the substrings rather than just the exact values?