Find Last Matching Key

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore a JavaScript function called findLastKey() that helps us find the last key in an object that matches a given condition. We'll learn how to use Object.keys(), Array.prototype.reverse(), and Array.prototype.find() to implement this function and see how it can be used to simplify our code when working with objects. By the end of this lab, you'll have a better understanding of how to work with JavaScript objects and how to use the findLastKey() function to make your code more concise and efficient.


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/destr_assign("`Destructuring Assignment`") subgraph Lab Skills javascript/variables -.-> lab-28305{{"`Find Last Matching Key`"}} javascript/data_types -.-> lab-28305{{"`Find Last Matching Key`"}} javascript/arith_ops -.-> lab-28305{{"`Find Last Matching Key`"}} javascript/comp_ops -.-> lab-28305{{"`Find Last Matching Key`"}} javascript/array_methods -.-> lab-28305{{"`Find Last Matching Key`"}} javascript/destr_assign -.-> lab-28305{{"`Find Last Matching Key`"}} end

Function to Find the Last Key that Matches a Condition

To find the last key in an object that satisfies a given condition, use the findLastKey function. This function takes an object and a testing function as arguments. If a matching key is found, the function returns it. Otherwise, it returns undefined. Here are the steps the function takes to find the last key:

  1. Use Object.keys() to get all the properties of the object.
  2. Use Array.prototype.reverse() to reverse the order of the keys.
  3. Use Array.prototype.find() to test the provided function for each key-value pair. The callback function receives three arguments - the value, the key, and the object.
  4. If a matching key is found, return it.
const findLastKey = (obj, fn) =>
  Object.keys(obj)
    .reverse()
    .find((key) => fn(obj[key], key, obj));

Here's an example of using findLastKey:

findLastKey(
  {
    barney: { age: 36, active: true },
    fred: { age: 40, active: false },
    pebbles: { age: 1, active: true }
  },
  (x) => x["active"]
); // 'pebbles'

To use this function, open the Terminal/SSH and type node to start practicing coding.

Summary

Congratulations! You have completed the Find Last Matching Key lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like