Iterate Over Object's Own Properties in Reverse

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore how to iterate over an object's own properties in reverse order using JavaScript. We will use built-in methods such as Object.keys() and Array.prototype.reverse() to achieve this. By the end of this lab, you will have a better understanding of how to work with objects in JavaScript and how to manipulate their properties.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/ToolsandEnvironmentGroup(["`Tools and Environment`"]) 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`") javascript/ToolsandEnvironmentGroup -.-> javascript/debugging("`Debugging`") subgraph Lab Skills javascript/variables -.-> lab-28598{{"`Iterate Over Object's Own Properties in Reverse`"}} javascript/data_types -.-> lab-28598{{"`Iterate Over Object's Own Properties in Reverse`"}} javascript/arith_ops -.-> lab-28598{{"`Iterate Over Object's Own Properties in Reverse`"}} javascript/comp_ops -.-> lab-28598{{"`Iterate Over Object's Own Properties in Reverse`"}} javascript/array_methods -.-> lab-28598{{"`Iterate Over Object's Own Properties in Reverse`"}} javascript/destr_assign -.-> lab-28598{{"`Iterate Over Object's Own Properties in Reverse`"}} javascript/debugging -.-> lab-28598{{"`Iterate Over Object's Own Properties in Reverse`"}} end

Here's how to iterate over an object's own properties in reverse

To iterate over an object's own properties in reverse order and run a callback for each one, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use Object.keys() to get all the properties of the object.
  3. Use Array.prototype.reverse() to reverse the order of the properties.
  4. Use Array.prototype.forEach() to run the provided function for each key-value pair.
  5. The callback function should have three arguments: the value, the key, and the object.

Here's the code:

const forOwnRight = (obj, fn) =>
  Object.keys(obj)
    .reverse()
    .forEach((key) => fn(obj[key], key, obj));

You can use this function with any object and callback function. For example, to log the values of { foo: 'bar', a: 1 } in reverse order, you can use the following code:

forOwnRight({ foo: "bar", a: 1 }, (v) => console.log(v)); // 1, 'bar'

Summary

Congratulations! You have completed the Iterate Over Object's Own Properties in Reverse lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like