0

im new to javascript and lets say i have an array of objects (books) like this:

let books = [{"pubDate":"2014", "authors":["John","Bob","Tom"], "name":"Book 1"}, {"pubDate":"2020", "authors":["Bob","Greg"], "name":"Book 2"}]
let authorsFrequency = getFrequency(books);
console.log(authorsFrequency) // expected result {"Bob":2,"Tom":1,"Greg":1,"John":1}

How could i properly build this getFrequency function? I've seen some similar questions like this one: Counting the occurrences / frequency of array elements

But not one as deep as my example.

Results order doesn't matter, infact the fastest the better since i need to loop through 30.000 books.

Thank you very much for your time.

jay2p
  • 75
  • 6
  • Flatten the authors - `books.flatMap(o => o.authors)` and then use the solutions in the answers you've added to count the items. – Ori Drori Feb 02 '21 at 00:33
  • @OriDrori it works! tyvm! How do i mark your answer as the the solution? – jay2p Feb 02 '21 at 00:43
  • You're welcome :) However, this is not a solution, just a comment. I'm closing your question as a duplicate because the answer exists in the link you've added. – Ori Drori Feb 02 '21 at 00:45

0 Answers0