Skip to content

Instantly share code, notes, and snippets.

@pchittum
Created March 1, 2023 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pchittum/4a4d29f0bb11bad1a11ea1d11a99e5a4 to your computer and use it in GitHub Desktop.
Save pchittum/4a4d29f0bb11bad1a11ea1d11a99e5a4 to your computer and use it in GitHub Desktop.
Scanning Blog page to find authors and blog post counts
<!-- sample DOM to be scanned by the JS below to pull out authors of articles written in 2022 and later
<main class="column">
<ul>
<li><article class="content is-clearfix"><h3><a href="/blog/article1">Article 1 Title</a></h3><p><span>Betty Farquharson</span> on <span>February 22nd 2023</span></p>Article 1 description text. <a class="is-pulled-right" href="/blog/article1">Read the rest of this post</a></article><hr>
</li>
<li><article class="content is-clearfix"><h3><a href="/blog/article2">Article 2 Title</a></h3><p><span>Jimmy Retalic</span> on <span>February 21st 2023</span></p>Article 2 description text. <a class="is-pulled-right" href="/blog/article2">Read the rest of this post</a></article><hr>
</li>
<li><article class="content is-clearfix"><h3><a href="/blog/article3">Article 3 Title</a></h3><p><span>Jimmy Retalic</span> on <span>February 21st 2022</span></p>Article 3 description text. <a class="is-pulled-right" href="/blog/article3">Read the rest of this post</a></article><hr>
</li>
<li><article class="content is-clearfix"><h3><a href="/blog/article4">Article 4 Title</a></h3><p><span>Emer Fitzpatrick</span> on <span>February 21st 2022</span></p>Article 4 description text. <a class="is-pulled-right" href="/blog/article4">Read the rest of this post</a></article><hr>
</li>
<li><article class="content is-clearfix"><h3><a href="/blog/article5">Article 5 Title</a></h3><p><span>Jimmy Retalic</span> on <span>February 21st 2020</span></p>Article 5 description text. <a class="is-pulled-right" href="/blog/article5">Read the rest of this post</a></article><hr>
</li>
<li><article class="content is-clearfix"><h3><a href="/blog/article6">Article 6 Title</a></h3><p><span>Jimmy Retalic</span> on <span>February 21st 2021</span></p>Article 6 description text. <a class="is-pulled-right" href="/blog/article6">Read the rest of this post</a></article><hr>
</li>
</ul>
</main>
/*
could be done much more elegantly, but laid out step by step to recollect what I did:
- retrieved the NodeList of 'LI' items
- cast the NodeList to Array so I could use the reduce() function
- pulled out all Author names on LI nodes with a date in 2022 or later and counted how many
*/
let mainCol = document.querySelector("main")
let articleList = mainCol.querySelector("ul")
let posts = articleList.querySelectorAll("li")
let postsArr = Array.from(posts)
let authorList = postsArr.reduce((accumulator, currentValue) => {
const totalCount = accumulator["Total"] ?? 0;
const details = currentValue.querySelectorAll("span");
const year = parseInt(details[1].innerText.split(" ")[2]);
if (year >= 2022){
const author = details[0].innerText;
const currCount = accumulator[author] ?? 0;
return {
...accumulator,
[author]: currCount + 1,
["Total"]: totalCount + 1,
}
} else {
return {...accumulator,}
}
}, {});
console.log(authorList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment