Unique Values in Array Based on Function

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore a JavaScript function that helps find all unique values in an array based on a provided comparator function. We will use the Array.prototype.reduce() and Array.prototype.some() methods to create a new array that contains only the first unique occurrence of each value, based on the provided comparator function. This lab will provide you with a deeper understanding of how to manipulate arrays in JavaScript.


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/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/obj_manip("`Object Manipulation`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") subgraph Lab Skills javascript/variables -.-> lab-28335{{"`Unique Values in Array Based on Function`"}} javascript/data_types -.-> lab-28335{{"`Unique Values in Array Based on Function`"}} javascript/arith_ops -.-> lab-28335{{"`Unique Values in Array Based on Function`"}} javascript/comp_ops -.-> lab-28335{{"`Unique Values in Array Based on Function`"}} javascript/cond_stmts -.-> lab-28335{{"`Unique Values in Array Based on Function`"}} javascript/obj_manip -.-> lab-28335{{"`Unique Values in Array Based on Function`"}} javascript/higher_funcs -.-> lab-28335{{"`Unique Values in Array Based on Function`"}} end

Finding Unique Values in an Array with a Function

To find all unique values of an array, provide a comparator function.

Use Array.prototype.reduce() and Array.prototype.some() to create an array containing only the first unique occurrence of each value. The comparator function fn takes two arguments, the values of the two elements being compared.

const uniqueElementsBy = (arr, fn) =>
  arr.reduce((acc, v) => {
    if (!acc.some((x) => fn(v, x))) acc.push(v);
    return acc;
  }, []);

To test the function, use the example below:

uniqueElementsBy(
  [
    { id: 0, value: "a" },
    { id: 1, value: "b" },
    { id: 2, value: "c" },
    { id: 1, value: "d" },
    { id: 0, value: "e" }
  ],
  (a, b) => a.id == b.id
); // [ { id: 0, value: 'a' }, { id: 1, value: 'b' }, { id: 2, value: 'c' } ]

Start practicing coding by opening the Terminal/SSH and typing node.

Summary

Congratulations! You have completed the Unique Values in Array Based on Function lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like