Mapping Unique Array Values to Object Keys

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the concept of creating an object with the unique values of an array as keys and their frequencies as the values. We will achieve this by making use of the Array.prototype.reduce() method to map unique values to an object's keys, adding to existing keys every time the same value is encountered. Through this lab, we will gain a deeper understanding of the reduce method and how it can be used to solve programming problems efficiently.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/destr_assign("`Destructuring Assignment`") subgraph Lab Skills javascript/variables -.-> lab-28689{{"`Mapping Unique Array Values to Object Keys`"}} javascript/data_types -.-> lab-28689{{"`Mapping Unique Array Values to Object Keys`"}} javascript/arith_ops -.-> lab-28689{{"`Mapping Unique Array Values to Object Keys`"}} javascript/comp_ops -.-> lab-28689{{"`Mapping Unique Array Values to Object Keys`"}} javascript/array_methods -.-> lab-28689{{"`Mapping Unique Array Values to Object Keys`"}} javascript/higher_funcs -.-> lab-28689{{"`Mapping Unique Array Values to Object Keys`"}} javascript/destr_assign -.-> lab-28689{{"`Mapping Unique Array Values to Object Keys`"}} end

Instructions for Counting Value Frequencies

To count the frequency of values in an array, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use the Array.prototype.reduce() method to map unique values to an object's keys, adding to existing keys every time the same value is encountered. This will create an object with the unique values of the array as keys and their frequencies as the values.
  3. The code for this operation is as follows:
const frequencies = (arr) =>
  arr.reduce((a, v) => {
    a[v] = a[v] ? a[v] + 1 : 1;
    return a;
  }, {});
  1. To use this function, call frequencies with the array as its argument. For example:
frequencies(["a", "b", "a", "c", "a", "a", "b"]); // { a: 4, b: 2, c: 1 }
frequencies([..."ball"]); // { b: 1, a: 1, l: 2 }

With these instructions, you can easily count the frequency of values in any given array.

Summary

Congratulations! You have completed the Value Frequencies lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like