{"id":12922,"library":"build-array","title":"Array Builder Utility","description":"The `build-array` package provides a minimalist utility for programmatically creating arrays of a specified `length`, optionally pre-filled with a given `value`. Currently stable at version 1.0.0, this package is likely in a maintenance state, receiving updates only for critical bug fixes if any, due to its straightforward and complete functionality. It offers a lightweight alternative to more verbose patterns like `new Array(length).fill(value)`, particularly useful in environments or coding styles where brevity and explicit initialization are preferred. Its primary differentiator is its single-function API, simplifying array creation and initial population without requiring chained methods or polyfills for older `Array.prototype.fill` behavior.","status":"maintenance","version":"1.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/ramitos/build-array","tags":["javascript","new","array","length","build","default","value"],"install":[{"cmd":"npm install build-array","lang":"bash","label":"npm"},{"cmd":"yarn add build-array","lang":"bash","label":"yarn"},{"cmd":"pnpm add build-array","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"CommonJS syntax, suitable for Node.js environments.","symbol":"buildArray","correct":"const buildArray = require('build-array');"},{"note":"ESM syntax. Given the package's age (v1.0.0), it likely uses a default export. Modern bundlers typically handle this, but explicit default import is the most reliable.","wrong":"import { buildArray } from 'build-array';","symbol":"buildArray","correct":"import buildArray from 'build-array';"}],"quickstart":{"code":"// CommonJS example in a Node.js environment\nconst buildArray = require('build-array');\n\n// Example 1: Create an array of 5 undefined elements\nconst arr1 = buildArray(5);\nconsole.log('Array 1 (undefined):', arr1); \n// Expected: [undefined, undefined, undefined, undefined, undefined]\n\n// Example 2: Create an array of 3 elements, all initialized to 0\nconst arr2 = buildArray(3, 0);\nconsole.log('Array 2 (filled with 0):', arr2); \n// Expected: [0, 0, 0]\n\n// Example 3: Create an array of 2 elements, filled with a string\nconst arr3 = buildArray(2, 'hello');\nconsole.log('Array 3 (filled with \"hello\"):', arr3); \n// Expected: ['hello', 'hello']\n\n// Example 4: Demonstrating the \"same reference\" gotcha when filling with objects\nconst obj = { id: 1 };\nconst arr4 = buildArray(2, obj);\narr4[0].id = 99; // Modifying the object in the first element\nconsole.log('Array 4 (object reference):', arr4); \n// Expected: [{ id: 99 }, { id: 99 }] - both elements updated because they share the same object reference\n\n// Example 5: Using it with dynamic length (modern JS syntax for nullish coalescing)\nconst maybeLength = Math.random() > 0.5 ? 4 : null;\nconst arr5 = buildArray(maybeLength ?? 0, 'dynamic');\nconsole.log('Array 5 (dynamic length):', arr5);","lang":"javascript","description":"This quickstart demonstrates how to use `build-array` to create arrays of various lengths, with and without initial fill values, and highlights a key behavior when filling with object references."},"warnings":[{"fix":"If independent objects are required for each array element, iterate over the created array and assign new object instances (e.g., `Array(length).map(() => ({...template}))`).","message":"When `buildArray` is used to fill an array with an object (or array) as the `value` argument, all elements in the resulting array will hold references to the *exact same object*. Modifying one element's object will affect all other elements.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For new projects targeting modern JavaScript environments, consider native methods like `Array(length).fill(value)` or `Array.from({ length }, () => value)` which offer similar functionality and are widely supported.","message":"This package primarily targets older Node.js environments (>=0.8). While it functions in modern environments, it might not leverage newer JavaScript features or optimizations available in `Array.prototype.fill` or `Array.from`.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For CommonJS, use `const buildArray = require('build-array');`. For ES Modules, use `import buildArray from 'build-array';`. Ensure your environment supports the chosen module system.","cause":"The `buildArray` function was not correctly imported or required. This often happens when mixing CommonJS `require()` with ES Module `import` syntax, or attempting to use a named import for a default export.","error":"TypeError: buildArray is not a function"},{"fix":"Ensure the first argument passed to `buildArray` is a non-negative integer. Use nullish coalescing (e.g., `length ?? 0`) or type checking if the length might be dynamic or optional.","cause":"The first argument, intended as the array's length, was not provided or evaluated to `undefined` or a non-numeric type, which `build-array` cannot process as a valid length.","error":"TypeError: The 'length' argument must be an integer, not undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}