Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
Storybook behavior of array.map() with specific array initialization

Context

I have this construct:

[...new Array(2)].map((value, index) => `item${index}`)

When running it from browser, node, or jest tests, it consistently produces:

['item0', 'item1']

If I just had the same without the array clone using the spread operator:

new Array(2).map((value, index) => `item${index}`)

Then I would get (as expected, due to how map works):

[undefined, undefined]

Reasons for the above are explained here (I did not know this before):

[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#description]

Problem

I had this bug - when using storybook, even when using array clone:

[...new Array(2)].map((value, index) => `item${index}`)

I get the following result:

[undefined, undefined]

Fixing the issue is not a problem, I get the proper result when including .fill():

[...new Array(2).fill(undefined)].map((value, index) => `item${index}`)

But the question is, why would the above work differently with storybook? Does anyone know this?

Storybook Config

I am using Nx Framework for project creation, and pretty much the default storybook config.

This is the in-project configuration for storybook:

const rootMain = require('../../../.storybook/main');

module.exports = {
  ...rootMain,

  core: { ...rootMain.core, builder: 'webpack5' },

  stories: [
    ...rootMain.stories,
    '../src/app/**/*.stories.mdx',
    '../src/app/**/*.stories.@(js|jsx|ts|tsx)',
  ],
  addons: [...rootMain.addons, '@nrwl/react/plugins/storybook'],
  webpackFinal: async (config, { configType }) => {
    // apply any global webpack configs that might have been specified in .storybook/main.js
    if (rootMain.webpackFinal) {
      config = await rootMain.webpackFinal(config, { configType });
    }

    // add your own webpack tweaks if needed

    return config;
  },
};

And this is the root storybook configuration referenced at the top of the above code block:

module.exports = {
  stories: [],
  addons: ['@storybook/addon-essentials'],
};

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!