Walk Through Object

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 a generator function that walks through all the keys of a given object using recursion. The purpose of this lab is to provide a hands-on experience for learners to understand how to use yield and yield* expressions in combination with for...of loops and Object.keys() to iterate over object keys and their values. By the end of this lab, learners will have a practical understanding of how to recursively walk through an object and generate an array of keys representing the current path and the corresponding values.


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/loops("`Loops`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28693{{"`Walk Through Object`"}} javascript/data_types -.-> lab-28693{{"`Walk Through Object`"}} javascript/arith_ops -.-> lab-28693{{"`Walk Through Object`"}} javascript/comp_ops -.-> lab-28693{{"`Walk Through Object`"}} javascript/cond_stmts -.-> lab-28693{{"`Walk Through Object`"}} javascript/loops -.-> lab-28693{{"`Walk Through Object`"}} javascript/array_methods -.-> lab-28693{{"`Walk Through Object`"}} javascript/spread_rest -.-> lab-28693{{"`Walk Through Object`"}} end

Code Walk Through Object Keys

To generate a list of all the keys of a given object, use the following steps:

  1. Open the Terminal/SSH and type node to start practicing coding.

  2. Define a generator function called walk that takes an object and an array of keys. Use recursion to walk through all the keys of the object.

  3. Inside the walk function, use a for...of loop and Object.keys() to iterate over the keys of the object.

  4. Use typeof to check if each value in the given object is itself an object. If the value is an object, use the yield* expression to recursively delegate to the same generator function, walk, appending the current key to the array of keys.

  5. Otherwise, yield an array of keys representing the current path and the value of the given key.

  6. Use the yield* expression to delegate to the walk generator function.

Here's the code:

const walkThrough = function* (obj) {
  const walk = function* (x, previous = []) {
    for (let key of Object.keys(x)) {
      if (typeof x[key] === "object") yield* walk(x[key], [...previous, key]);
      else yield [[...previous, key], x[key]];
    }
  };
  yield* walk(obj);
};

To test the code, create an object and use the walkThrough function to generate a list of all its keys:

const obj = {
  a: 10,
  b: 20,
  c: {
    d: 10,
    e: 20,
    f: [30, 40]
  },
  g: [
    {
      h: 10,
      i: 20
    },
    {
      j: 30
    },
    40
  ]
};
[...walkThrough(obj)];
/*
[
  [['a'], 10],
  [['b'], 20],
  [['c', 'd'], 10],
  [['c', 'e'], 20],
  [['c', 'f', '0'], 30],
  [['c', 'f', '1'], 40],
  [['g', '0', 'h'], 10],
  [['g', '0', 'i'], 20],
  [['g', '1', 'j'], 30],
  [['g', '2'], 40]
]
*/

Summary

Congratulations! You have completed the Walk Through Object lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like