Flattening Nested Data Structures with JavaScript Iterators

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will be exploring the concept of iterators and generators in JavaScript. Specifically, we will be focusing on creating a flat iterator that can iterate over nested arrays and sets, and flatten them into a single iterable. This will allow us to efficiently access and manipulate nested data structures in a more streamlined manner. By the end of the lab, you will have a deeper understanding of how generators and iterators work, and how they can be applied to solve real-world problems.


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/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/loops("`Loops`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28310{{"`Flattening Nested Data Structures with JavaScript Iterators`"}} javascript/data_types -.-> lab-28310{{"`Flattening Nested Data Structures with JavaScript Iterators`"}} javascript/arith_ops -.-> lab-28310{{"`Flattening Nested Data Structures with JavaScript Iterators`"}} javascript/cond_stmts -.-> lab-28310{{"`Flattening Nested Data Structures with JavaScript Iterators`"}} javascript/loops -.-> lab-28310{{"`Flattening Nested Data Structures with JavaScript Iterators`"}} javascript/spread_rest -.-> lab-28310{{"`Flattening Nested Data Structures with JavaScript Iterators`"}} end

Flat Iterator Explanation

To create a generator that iterates over an iterable and flattens nested iterables, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use recursion in the generator function.
  3. Use a for...of loop to iterate over the values of the given iterable.
  4. Use Symbol.iterator to check if each value is an iterable.
  5. If it is, use the yield* expression to recursively delegate to the same generator function.
  6. Otherwise, yield the current value.

Here is an example code snippet:

const flatIterator = function* (itr) {
  for (let item of itr) {
    if (item[Symbol.iterator]) yield* flatIterator(item);
    else yield item;
  }
};

const arr = [1, 2, [3, 4], [5, [6, [7], 8]], 9, new Set([10, 11])];
[...flatIterator(arr)]; // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

In the example, arr is an array of values, including nested arrays and a set. The flatIterator generator function is used to flatten these nested values and return a flattened array.

Summary

Congratulations! You have completed the Flat Iterator lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like