Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Counting Occurrences Value Substrings contained in entire Object

I want to count substrings of object values, i.e. the entire object contains a string instead of a key equals a string. A working Xpath in XSLT would be

count(//v[contains(.,current-grouping-key())])

But i can't figure this out in javascript.

I tried 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)

But this counts the exact strings only. So I get:

"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 instead of the exact values?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you can use that:

const obj = 
  [ { v: 'Bla Blu Bli' }
  , { v: 'Bla Blu Bli' }
  , { v: 'Bla Blu'     }
  , { v: 'Bla Bli'     }
  ];

const counts = obj
  .map(e=>e.v.split(' ').sort((a,b)=>a.localeCompare(b)))
  .reduce((r,a,_,all)=>
    {
    let terms = a.join(' ')
    if (!r[terms])
      r[terms] = all.reduce((c,x)=>c+(a.every(v=>x.includes(v))?1:0),0);
    return r
    },{})
    
console.log(  counts )
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

about 4 years ago · Juan Pablo Isaza Report

0

This second version should be faster.

(you wrote I have 100k+ values , in a comment)

It creates an array only of different series, associated with the number of copies of identical series
And traverses this array by adding to this quantity those of the other sets including the same values,
by selecting only those whose size is larger.

I used Set elements because according to the doc a [set].has(value) is faster than an [array].includes(value)

const obj = 
  [ { v: 'Bla Blu Bli' }
  , { v: 'Bla Bli Blu' }
  , { v: 'Bla Blu'     }
  , { v: 'Bla Bli'     }
  ];

const counts = obj
  .reduce((r,o) => // create arr with unique sets with count of copies
    {
    let 
      arr = o.v.split(' ')
    , sam = r.find(x=>(x.s.size===arr.length) && arr.every(a=>x.s.has(a)) )
      ;
    if (sam)  ++sam.n   // + one more copy
    else      r.push({arr, s:new Set(arr), n:1 })
       // next step need  arr and set to avoid losing time 
       // in conversion operations between array and Set
    return r
    },[]) 
  .reduce((c,e,_,all) =>
    {
    c[e.arr.join(' ')] = e.n  
      + all.reduce((s,x)=>((x.s.size > e.s.size && e.arr.every(a=>x.s.has(a))) ? s + x.n : s),0)
      // try to find includings, only in largest sets
    return c
    },{})  

console.log(  counts  )
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

about 4 years ago · Juan Pablo Isaza Report

0

You have to use indexOf or similar, to see if a substring exists in a string.

Example:

obj = [
    {
        "v": "Bla † Blu † Bli"
    },
    {
        "v": "Bla † Blu † Bli"
    },
    {
        "v": "Bla † Blu"
    }
]

const counts = Object.fromEntries(
  obj.map(({v}) => [v, obj.reduce((acc, el) => {
    if (el.v.indexOf(v) > -1) acc++;
    return acc;
  }, 0)])
);

console.log(counts);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!