Question

Is there a difference between:

 var samples = {
        "TB10152254-001": {
            folderno: "TB10152254",
            ordno: "001",
            startfootage: "",
            endfootage: "",
            tagout: "Y"
        },
        "TB10152254-002": {
            folderno: "TB10152254",
            ordno: "002",
            startfootage: "",
            endfootage: "",
            tagout: "Y"
        },

        "TB10152254-003": {
            folderno: "TB10152254",
            ordno: "003",
            startfootage: "",
            endfootage: "",
            tagout: "Y"
        }
    };

AND

 var samples = new Array();
samples["TB10152254-001"]  = {
            folderno: "TB10152254",
            ordno: "001",
            startfootage: "",
            endfootage: "",
            tagout: "Y"};

samples["TB10152254-002"] = {
            folderno: "TB10152254",
            ordno: "002",
            startfootage: "",
            endfootage: "",
            tagout: "Y"
        };

samples["TB10152254-003"] =  {
            folderno: "TB10152254",
            ordno: "003",
            startfootage: "",
            endfootage: "",
            tagout: "Y"
        };

EDIT:

I will re-phrase the question: How do I populate the hash dynamically? I can't do something like samples.TB10152254-003 because i TB10152254-003 is dynamic...so, is that even possible?

Was it helpful?

Solution

Both will work because an Array is a type of object. But there isn't any advantage to using an Array this way, and can easily give trouble when you iterate over the properties using a for/in.

The Object would be the proper type to use for named properties. Reserve your use of Array for index properties only.


With regard to your edit, you can dynamically populate the Object the same way as the Array, using square bracket notation.

   // Create a new empty object. You an use "new Object()" if you wish
var samples = {};

  // Populate the "samples" object in the same way you would an Array.
samples["TB10152254-001"]  = {
            folderno: "TB10152254",
            ordno: "001",
            startfootage: "",
            endfootage: "",
            tagout: "Y"};

samples["TB10152254-002"] = {
            folderno: "TB10152254",
            ordno: "002",
            startfootage: "",
            endfootage: "",
            tagout: "Y"
        };

samples["TB10152254-003"] =  {
            folderno: "TB10152254",
            ordno: "003",
            startfootage: "",
            endfootage: "",
            tagout: "Y"
        };

OTHER TIPS

Yes. In the second example example, you are "abusing" the fact that an Array as also an Object. Don't do this.

Use Arrays for numerical indexed values only and plain Objects for kinda hash tables.

I suggest to read more about Arrays and Objects.

In JavaScript, basically everything is an object. Also arrays. But the Array object provides additional methods for dealing with numerical indexed data.

You can probably see the difference the best when you do samples.length in the second example. A plain object has no property length, an array does. For an array, it tells you the number of elements stored in the array. Now when you call samples.length in the second example, you will get 0 because the array actually holds no element.

What might lead to more confusing is the fact, that you have two possibilities to access object properties: The "dot notation", object.property and the "array notation", object['property']. But this is a functionality of objects not of arrays.

The array notation comes in handy, when you generate the keys or have the property name stored in a variable.

Update:

As written, you can make use of the array notation to create properties dynamically, e.g.:

var samples = {};

for(var i = 0; i < 4; i++) {
    samples["TB10152254-" + i] = {
        folderno: "TB10152254",
        ordno: i,
        startfootage: "",
        endfootage: "",
        tagout: "Y"
    }
}

If you want to access the properties, you have to use the for...in loop to iterate over the keys:

for(var key in samples) {
    var value = samples[key];
}

But note: Never ever use for...in to loop over an array. On the page I linked to is also written why.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top