Reverse Unique Array Elements Using JavaScript

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore how to use the uniqueElementsByRight() function in JavaScript to find the reversed unique values of an array based on a provided comparator function. We will learn how to use Array.prototype.reduceRight() and Array.prototype.some() methods to create an array containing only the last unique occurrence of each value based on the comparator function provided. By the end of this lab, you will have a better understanding of how to manipulate arrays in JavaScript using these methods.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic 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`") subgraph Lab Skills javascript/variables -.-> lab-28338{{"`Reverse Unique Array Elements Using JavaScript`"}} javascript/data_types -.-> lab-28338{{"`Reverse Unique Array Elements Using JavaScript`"}} javascript/arith_ops -.-> lab-28338{{"`Reverse Unique Array Elements Using JavaScript`"}} javascript/comp_ops -.-> lab-28338{{"`Reverse Unique Array Elements Using JavaScript`"}} javascript/cond_stmts -.-> lab-28338{{"`Reverse Unique Array Elements Using JavaScript`"}} javascript/obj_manip -.-> lab-28338{{"`Reverse Unique Array Elements Using JavaScript`"}} end

Function to Find Reversed Unique Values in Array

To find all the unique values of an array based on a provided comparator function from the right, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use Array.prototype.reduceRight() and Array.prototype.some() to create an array containing only the last unique occurrence of each value, based on the comparator function fn.
  3. The comparator function takes two arguments: the values of the two elements being compared.
  4. Here's the code to implement the function:
const uniqueElementsByRight = (arr, fn) =>
  arr.reduceRight((acc, v) => {
    if (!acc.some((x) => fn(v, x))) acc.push(v);
    return acc;
  }, []);
  1. Use the following code to test the function:
uniqueElementsByRight(
  [
    { 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: 'e' }, { id: 1, value: 'd' }, { id: 2, value: 'c' } ]

Summary

Congratulations! You have completed the Reversed 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