4

Here is where I am stuck. I want to take this statement and revise it in a manner that the empty array I fill (which I surmise might not work with dynamic values), will initialize bucket to the n distinct empty arrays.

How do I do this? Is there a way to make this fill method behave in the intended manner?

let radix = 10;
let badBucket = [...Array(radix).fill([])];
let goodBucket = JSON.parse(JSON.stringify([...Array(radix).fill([])]));
badBucket[3].push(33);
goodBucket[3].push(33);
console.log(JSON.stringify(badBucket));
console.log(JSON.stringify(goodBucket));
Vahe
  • 1,699
  • 3
  • 25
  • 76
  • What's wrong with your current code? – Spectric Sep 19 '21 at 17:07
  • If I were to push an item to bucket[0] like so bucket[0].push(1), the array contents would all have 1 [[10],[10],[10],[10],[10],[10],[10],[10]], which pretty much be undesireable – Vahe Sep 19 '21 at 17:11
  • Is this one of those scenarios where If fill can accept a callback, I can return a new empty array from the callback, like so... fill(() => []) fingers crossed...awww, unfortunately that was not the case as hoped – Vahe Sep 19 '21 at 17:13
  • Ok, I have successfully reached desired result, with a more verbose one liner, If helpful to others I can post as a separate answer or keep my comment logs in the comments. If any more concise initialization expression exists than this, please provide a new answer to my comments. – Vahe Sep 19 '21 at 17:17
  • 1
    `const length = 10, bucket = Array.from({ length }).map(() => []);` – Peter Seliger Sep 19 '21 at 17:21
  • It looks like my callback to map was what I as unsuccessfully trying to pass into fill (where fill can only understand static values of array length) – Vahe Sep 19 '21 at 17:22
  • If I can relabel the JSON.parse and JSON.stringify, I might be able to cut down the character length of my one-line statement.... – Vahe Sep 19 '21 at 17:26
  • 1
    @VaheJabagchourian ... It's not about *one-liners* or being this kind of *extra smart* . A practical solution very often is just a good enough balanced compromise in between a straightforward as possible but still readable / maintainable implementation. – Peter Seliger Sep 19 '21 at 17:34
  • It seems a looping initializer solution would be a little verbose here, the proposed fill method I proposed much more concise, but of course a map solution like you proposed would be easiest to understand. – Vahe Sep 19 '21 at 17:40

2 Answers2

4

You can use the callback function of Array.from.

let length = 5;
let res = Array.from({length}, _=>[]);
console.log(res);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • Thank you for the specific alternate version using underscore object, this is quite desirable and produces a great readable response, with an object of 5 length that I can infer is assigned to an empty array. – Vahe Sep 19 '21 at 18:58
1

Try:

let radix = 10;
let bucket = [...Array(radix)].map(e=>[])
bucket[0].push(1)
console.log(JSON.stringify(bucket));
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • 1
    This is exactly is the shortest length one liner that meets my desire. Thank you. Now I know which of the two functions (map ,fill) will accept the callback, and corresponding equivalent initialization stements. – Vahe Sep 19 '21 at 17:35