My goal is to analyze an array of user objects, some of which are duplicated. The objective is to determine the frequency of each unique user object within the array and reorganize them so that the most frequently occurring user comes first while the least common user is placed at the end.
const users =
[
{id: "0f933bbd-d1fb-4ad5-80f0-661c3c0aa2f8", handle: "lisa", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8c4c1dbc9e8cfc5c9c1c486cbc7c5">[email protected]</a>", createdAt: 1593894321997},
{id: "bbfc927c-f3d4-4cdd-b872-9cb233a194aa", handle: "jisoo", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7cdced4c8c8e7c0cac6cecb89c4c8ca">[email protected]</a>", createdAt: 1592452421714},
{id: "be942039-6a59-46a4-9f92-1f7808b20c2f", handle: "unnieJennie", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6aca3a8a8afa386a1aba7afaae8a5a9ab">[email protected]</a>", createdAt: 1593894227232},
{id: "0f933bbd-d1fb-4ad5-80f0-661c3c0aa2f8", handle: "lisa", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e12170d1f3e19131f1712501d1113">[email protected]</a>", createdAt: 1593894321997},
{id: "1d1e31cd-eff2-47de-b46d-4d45bb2dd97f", handle: "kawikaLovesKpop", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d464c5a44464c6d4a404c4441034e4240">[email protected]</a>", createdAt: 1593894550566},
{id: "be942039-6a59-46a4-9f92-1f7808b20c2f", handle: "unnieJennie", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97fdf2f9f9fef2d7f0faf6fefbb9f4f8fa">[email protected]</a>", createdAt: 1593894227232},
{id: "0f933bbd-d1fb-4ad5-80f0-661c3c0aa2f8", handle: "lisa", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c70756f7d5c7b717d7570327f7371">[email protected]</a>", createdAt: 1593894321997},
{id: "1d1e31cd-eff2-47de-b46d-4d45bb2dd97f", handle: "kawikaLovesKpop", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="630802140a080223040e020a0f4d000c0e">[email protected]</a>", createdAt: 1593894550566},
{id: "be942039-6a59-46a4-9f92-1f7808b20c2f", handle: "unnieJennie", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fe5eae1e1e6eacfe8e2eee6e3a1ece0e2">[email protected]</a>", createdAt: 1593894227232}
]
To achieve this, I attempted using the countBy method from lodash:
const bestMatched = countBy(users, "handle");
After running the code, the result showed:
{lisa: 3, jisoo: 1, unnieJennie: 3, kawikaLovesKpop: 2}
However, it is important for me to retain access to the original users array with all key-value pairs intact.