Learn Advanced Array Methods by Building a Statistics Calculator

object.keys() returns an array of keys not values
my question is: why sort and filter methods can access the values of these keys?
or the keys contains the values somehow…!
sorry if i sounded stupid but when i try to log the object.key(counts) alone and both highest and mode ,it seems like they get the values somehow from an array of keys only.

const counts = { '1': 11, '2': 5, '4': 11 };
const highest = Object.keys(counts).sort(
    (a, b) => counts[b] - counts[a]
  )[0];
const mode = Object.keys(counts).filter(
    (el) => counts[el] === counts[highest]
  );
console.log(mode);

notice like you are using counts[b] and counts[a] and counts[el] inside the functions. You are using the keys to get the values

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.