Question

I'm trying to create an associative array in javascript where an array is filled with dynamic data located in attr fields of checkboxes - called "path" and "name". I want to make it so that every time a checkbox is checked, its attr "path" is an index - as there are five checkboxes that can be checked that can be associated with that certain path. I want the array to be something like this if possible where the "name" attr is associated with the path ["path1","name1","name2","name3","name4","name5","path2","name1ofpath2"] I tried using objects, but when I check the checkboxes I only get an alert saying "[object Object]"..... any ideas? perhaps my wording is confusing?

    var array = {};
            $(".checkbox").bind('change',function() {

                            var path = $(this).attr("path");
                            var name = $(this).attr("name");
                            array[path] = name; 
Was it helpful?

Solution

The problem is not with your code, but with alert, which when passed an object will invoke its .toString() method which typically reports [object Object]. Try using console.dir() instead.

As for your object itself, what you have when you use {} is an object whose properties are equivalent to the keys of the associate arrays available in other languages, i.e.

var obj = {
    path1: name1,
    path2: name2,
    ...
};

It's not really an "array" at all.

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