4

Bash can create a sparse array in one line

names=([0]="Bob" [1]="Peter" [20]="$USER" [21]="Big Bad John")

§ Creating Arrays

Can JavaScript create a sparse array like this?

4
  • No, I don't think it can; but I'd like to be proven wrong (if only for academic interest). I'm assuming that creating a function you could call to do this (or extending a prototype) wouldn't qualify as meeting your requirement? Commented Aug 9, 2014 at 20:29
  • 1
    Does it count if you wrap it in an IIFE names = (function(a) { a[2] = 'Bob'; a[22] = 'Fred'; return a;})([]); -> jsfiddle.net/adeneo/ne3yjoLx Commented Aug 9, 2014 at 21:08
  • Then no, there's no native way to do this, in javascript declarations like in your example code would have to be seperated by semicolons, as spaces wouldn't really count as closing in JS. Commented Aug 9, 2014 at 21:25
  • @adeneo I think you have misunderstood the question. I am asking if JavaScript has an analog to the Bash example above, not a character for character identical command. Commented Aug 9, 2014 at 21:28

2 Answers 2

7

Technically, JavaScript has a sparse array literal, but it's cumbersome.

var x = [8,,,,4,,,,6]

I don't think you would want to use that as you wouldn't want to count the commas between [1] and [20]. Using objects instead of arrays seems more natural for JavaScript in this case.

var names = {0: "Bob", 1: "Peter", 20: "$USER", 21: "Big Bad John"};

Whether you quote the integers or not, you get the same result -- they're converted to strings to be used as keys in the object (which is basically a hash). (Oh, and I'm not doing anything with your shell variable here.) The same is true for access with []. If you look up names[0] it is actually the same as names['0'] in this case.

However, if you want an actual array, a possible sparse-array-creation function is:

function sparseArray() {
    var array = [];
    for (var i = 0; i < arguments.length; i += 2) {
        var index = arguments[i];
        var value = arguments[i + 1];
        array[index] = value;
    }
    return array;
}
var names = sparseArray(0, "Bob", 1, "Peter", 20, "$USER", 21, "Big Bad John");

There's no error checking, so leaving off the final argument would set 21 -> undefined and giving a non-integer key will add it as an object property instead...

Sign up to request clarification or add additional context in comments.

Comments

1

In ES2015+, you can create an empty array and then use Object.assign() to populate it sparsely:

const names = Object.assign([],
    {0: "Bob", 1: "Peter", 20: "$USER", 21: "Big Bad John"});

This should functionally do exactly the same thing as Jason S's sparseArray() (which is needed before ES2015).


I'll note it is also technically possible to create it as an object and then reset its prototype with Object.setPrototypeOf(names, Array.prototype), but that is likely to interfere with JS engine optimizations down the line because the result wasn't initially created as an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.