DEV Community

ivkeMilioner
ivkeMilioner

Posted on • Edited on

1 2

Counting phrase in string.

I found a way to find repetitive words and count that words in the text. But I can't find a way to find and count phrases in the text. Do you have any solution?

https://jsitor.com/nhY17bVsu

let str = `Tennis superstar Novak Djokovic has pulled out of the ATP Cup in Sydney, amid speculation about his vaccination status and whether he will defend his Australian Open title next month. World No.1 Novak Djokovic has withdrawn from the 2022 ATP Cup,” organisers said in a statement on Wednesday, ahead of the start of the tournament on Saturday.ennis superstar Novak Djokovic has pulled out of the ATP Cup in Sydney, amid speculation about his vaccination status and whether he will defend his Australian Open title next month. World No.1 Novak Djokovic has withdrawn from the 2022 ATP Cup,” organisers said in a statement on Wednesday, ahead of the start of the tournament on Saturday.`
let names = str.split(' ')

const count = names =>
  names.reduce((a, b) => ({
    ...a,
    [b]: (a[b] || 0) + 1
  }), {}) // don't forget to initialize the accumulator

const duplicates = dict =>
  Object.keys(dict).filter((a) => dict[a] > 1)

console.log(count(names)) // { Mike: 1, Matt: 1, Nancy: 2, Adam: 1, Jenny: 1, Carl: 1 }
console.log(duplicates(count(names))) // [ 'Nancy' ]


Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (1)

Collapse
 
benjioe profile image
Benjioe • Edited

Split on dots ?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more