1

I have an array of objects, each object has an unique id and I want to link multiple (sometimes hundreds) extensions to an object linked by it's id.

Here a psuedo code example:

var objects = [
    {id:15,name:"client John Doe"},
    {id:28,name:"server"}
]

var extensions = [
    28:[
        {
            name:"watch errors",
            command: "tail -f error.log"
        },
        {
            name:"clear errors",
            command: "> error.log"
        }
    ]
]

This is not valid javascript, what is the best alternative to create an numeric associative array?

3 Answers 3

1

Just use an object literal instead of an array:

var extensions = {
    28: […]
};

You can access it by extensions[28] (or extensions["28"]).

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

Comments

0

You have two main options:

Use an Object

var extensions = {
    28:[
        {
            name:"watch errors",
            command: "tail -f error.log"
        },
        {
            name:"clear errors",
            command: "> error.log"
        }
    ] };
console.log(extensions[28][0].name); // "watch errors"

Note that you can't use extensions.28 to access this, you need to use extensions[28].

Use a Sparse Array

The array you're looking for here is perfectly legal...you just can't specify it as a literal.

var extensions = [];
extensions[28] = [
            {
                name:"watch errors",
                command: "tail -f error.log"
            },
            {
                name:"clear errors",
                command: "> error.log"
            }
        ];

console.log(extensions[28][0].name); // "watch errors"

Now you have an array, with a value at index 28, and indices 0-27 undefined.

Now, it's worth noting as a caveat here that lots of people hate sparse arrays in javascript. Also, you'll need to be careful iterating through them, accessing length, etc. But they ARE a valid part of the language, and this is one use-case for them.

Comments

0

This should work if you use object notation. I would also recommend enclosing the 28 in quotes. This will help avoid potential confusion of thinking it is an array index when it is not.

var objects = [
    {id:15,name:"client John Doe"},
    {id:28,name:"server"}
]

var extensions = {
    "28":[
        {
            name:"watch errors",
            command: "tail -f error.log"
        },
        {
            name:"clear errors",
            command: "> error.log"
        }
    ]
}

You could then read the property as extensions["28"][0], you can't do extensions.28[0] however.

6 Comments

You mean: var extensions = { "28":[]}
Indeed, corrected it. That's what I get for not testing first. Also not 100% sure this is what the you want to do, but we'll see!
There is no need for quotes.
Quotes are a good practice when using non-standard identifiers. It helps distinguish from array indices or other possible points of confusion. I'd also recommend accessing it as extensions["28"] in most cases to again prevent issues. You are correct though, in the case of numbers it isn't strictly required.
"Quotes are a good practice when using non-standard identifiers" That's the first time I here about this good practice. Either way, your answer suggests that quotes are required, which is not the case (and the point of my comment).
|

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.