How do you write an empty array?
Ryan Kahn

Ryan Kahn @shiftyp

About: I solve computer problems as a dev, human problems as a teacher

Location:
New York City, USA
Joined:
Oct 25, 2018

How do you write an empty array?

Publish Date: Dec 31 '19
8 10

How do you write empty arrays when they have multiple dimensions? Answer in a comment or in the twitter poll below 👇

Comments 10 total

  • Ryan Kahn
    Ryan KahnDec 31, 2019

    I like it. Very empty

  • Mourad Bougarne
    Mourad BougarneDec 31, 2019

    The samplist way is :

    let myArr = [[],[],[]]

    With spread operator:

    let arr1 = [[],[]]
    let arr2 = [[],...arr1]

    We can use it with new Array(3) fill() and map()

  • Mourad Bougarne
    Mourad BougarneDec 31, 2019

    Did I get the challenge wrong? It Isn't about creating empty multidimensional array?

    • Ryan Kahn
      Ryan KahnDec 31, 2019

      It's an interesting challenge. Your answer isn't wrong per-se, as how you represent the empty array depends on the code that processes the value. I think your answer however may show a mix-up between the dimension of an array vs. the length of an array. A multidimensional array is nested according to the number of dimensions. So a two dimensional array with two rows and two columns might look like:

      [[1,2],[3,4]]
      

      The number of row and columns are the lengths of the arrays in each dimension. A three dimensional array with one element might look like

      [[[1]]]
      

      So when I see an array [[],[],[]] I see a two dimensional array, with a length of three in the first dimension. Make sense?

      • Mourad Bougarne
        Mourad BougarneDec 31, 2019

        Thank you Ryan!!! You teach me something new. i think here in this example: [[],[],[]] it's a 3d array?!

        • Ryan Kahn
          Ryan KahnDec 31, 2019

          You can tell how many dimensions an array has by how many individual indices you need to access the values. let's add some values to the above array and access one:

          const arr = [[1],[2],[3]]
          
          const value = arr[0][0] // value is 1
          

          Notice that we need two indices to access the values. An array of three dimensions would be nested such that you need three indices. For example:

          const arr = [[[1],[2]],[[3],[4]],[[5],[6]]]
          
          const value = arr[0][0][0] // value is 1
          
          • Mourad Bougarne
            Mourad BougarneDec 31, 2019

            Thanks so much Ryan, it makes a lot of sense now.

  • Carlos Aguilar
    Carlos AguilarJan 1, 2020

    If the challenge/problem specified the cardinality of the sets each of the dimensions span, we could use a recursive function, something like:

    type Space<T> = T | Array<Space<T>>
    
    function initSpace<T>(identity: T, ...cardinalities: number[]): Space<T> {
      if (!cardinalities.length) return identity
    
      let cardinality = cardinalities[0]
      cardinalities = cardinalities.slice(1)
    
      return new Array(cardinality)
        .fill(null)
        .map(() => initSpace(identity, ...cardinalities))
    }
    

    Let's say you want that three-dimensional array to be Rubik's cube-like, filled with zeros:

    let numbers: Space<number> = initSpace<number>(0, 3, 3, 3)
    

    Otherwise, I'd simply go for the solution @wingkwong posted: [] 🤷‍♂️

Add comment