Kevin Boroumand
Kevin Boroumand

Reputation: 37

Remove everything before comma from output of sorted array

Here is my code. The output equals "Bothell,4". What can I do to make the output "Bothell" without the comma and count portion?

let As = document.getElementsByTagName('a');

let towns = new Map();

for(let a of As)
{
  let town = a.textContent.split(',')[1].trim()
  if(towns.has(town))
  {
    towns.set(town, towns.get(town)+1) 
  }
  else
  {
    towns.set(town, 1);
  }
}

let most = [...towns.entries()].sort((a, b) => b[1] - a[1])[0]

console.log(most);
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bellevue</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Kirkland</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Monroe</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>

Upvotes: 0

Views: 51

Answers (2)

user120242
user120242

Reputation: 15268

Just most[0] would have fixed your code.

Changed to just track maximum count.

let As = document.getElementsByTagName('a');

let towns = new Map();
let max = [];

for(let a of As)
{
  let town = a.textContent.split(',')[1].trim()
  if(towns.has(town))
  {
    const count = towns.get(town)+1;
    towns.set(town, count);
    max[count] = town;
  }
  else
  {
    towns.set(town, 1);
    max[1] = town;
  }
}

let most = max.pop()

console.log(most);
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bellevue</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Kirkland</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Monroe</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>

Upvotes: 1

KooiInc
KooiInc

Reputation: 122936

[...towns.entries()] is an Array of Array(2). Just pick the first element of the first element of the sorted. You don't need the entries-part by the way, just spread the Map to an Array.

Alternatively you can use a reducer to create a frequency table, included that in te snippet.

let towns = new Map();

document.querySelectorAll('a').forEach(href => {
  const town = href.textContent.split(',')[1].trim();
  if(towns.has(town)) {
    towns.set(town, towns.get(town)+1) 
  } else {
    towns.set(town, 1);
  }
});

const most = [...towns].sort((a, b) => b[1] - a[1])[0][0];
//                                                    ^ here
console.log(most);

// alternative use reduce to create a frequency table
const freqs = [...document.querySelectorAll('a')]
 .reduce( (acc, val) => {
   const town = val.textContent.split(',')[1].trim();
   return {...acc, [town]: acc[town] ? acc[town] + 1 : 1 };
  }, {});
// now you need entries ;)
console.log(Object.entries(freqs).sort( (a, b) => b[1] - a[1] )[0][0]);
a {display: block}
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bellevue</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Kirkland</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Monroe</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>

Upvotes: 0

Related Questions