Initialize N-Dimensional Array

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore how to create n-dimensional arrays with a given value using recursion in JavaScript. We will use the Array.from() and Array.prototype.map() methods to generate rows where each is a new array initialized using initializeNDArray(). By the end of this lab, you will have a solid understanding of how to create multi-dimensional arrays in JavaScript and how to use recursion to accomplish this task.


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/BasicConceptsGroup -.-> javascript/obj_manip("`Object Manipulation`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28397{{"`Initialize N-Dimensional Array`"}} javascript/data_types -.-> lab-28397{{"`Initialize N-Dimensional Array`"}} javascript/arith_ops -.-> lab-28397{{"`Initialize N-Dimensional Array`"}} javascript/comp_ops -.-> lab-28397{{"`Initialize N-Dimensional Array`"}} javascript/array_methods -.-> lab-28397{{"`Initialize N-Dimensional Array`"}} javascript/obj_manip -.-> lab-28397{{"`Initialize N-Dimensional Array`"}} javascript/higher_funcs -.-> lab-28397{{"`Initialize N-Dimensional Array`"}} javascript/spread_rest -.-> lab-28397{{"`Initialize N-Dimensional Array`"}} end

How to Initialize an N-Dimensional Array in JavaScript

To create an N-dimensional array in JavaScript, you can use the initializeNDArray function. This function takes a value and any number of dimensions as arguments and returns a new array initialized with that value.

To use initializeNDArray, you can follow these steps:

  1. Open the Terminal/SSH and type node to start coding.
  2. Use recursion to create the array with the given number of dimensions.
  3. Use Array.from() and Array.prototype.map() to generate rows where each row is a new array initialized using initializeNDArray().

Here's the code for the initializeNDArray function:

const initializeNDArray = (val, ...args) =>
  args.length === 0
    ? val
    : Array.from({ length: args[0] }).map(() =>
        initializeNDArray(val, ...args.slice(1))
      );

You can then call initializeNDArray with the desired value and number of dimensions. For example:

initializeNDArray(1, 3); // [1, 1, 1]
initializeNDArray(5, 2, 2, 2); // [[[5, 5], [5, 5]], [[5, 5], [5, 5]]]

Summary

Congratulations! You have completed the Initialize N-Dimensional Array lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like