0

I have an associative array where the keys are strings and the values are arrays on integers.

    {"Remebering":[0,0,0,0,0,0,0,0,0,0,0],
     "Understanding":[0,0,0,0,0,0,0,0,0,0,0],
     "Analyzing":[0,0,0,0,0,0,0,0,0,0,0]}

I am looping through each object and then updating values in the array.

var rigorLabels = $('.subHeader', standardsTable);
    var itemCounts = new Array($('.itemTypeLabel', standardsTable).length);
    for (var i = 0; i < itemCounts.length; i++)
    {
        itemCounts[i] = 0;
    }

    var rigorArray = [];
    for (var i = 1; i < rigorLabels.length; i++)
    {
        rigorArray[rigorLabels[i].innerHTML.toString()] = itemCounts;
    }
$('.itemTypeLabel', standardsTable).each(function (itemIndex)
{
    itemTypes += '"' + $(this).html() + '",';
    var itemTypeName = $(this).html().replace(' ', '').replace('/', '');

    $('.subHeader:not(:First)', standardsTable).each(function() {

        var rigorName = $(this).html().toString();
        var itemInput = $('#' + $(this).html() + '_' + itemTypeName + '_input');
        if(typeof (itemInput.attr('value')) != 'undefined' && itemInput.attr('value') != '')
            rigorArray[rigorName][itemIndex] = parseInt(itemInput.attr('value'));
    });
});

Everything seems to be working in the above code except when I hit the code to update rigorArray. At this point I should be updating a single object and a single value in array of integers. The problem I'm having is that it's updating all objects. So if I'm trying to update the 2 position for the Remembering object, the update is setting the value in the second position for all objects, not just Remembering.

RUEMACHINE
  • 381
  • 2
  • 9
  • 23
  • It would be useful to have a [code snippet](http://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) or [jsFiddle](http://jsfiddle.net/) to see the behaviour – James Thorpe Dec 08 '14 at 12:28
  • Also, [be careful](http://stackoverflow.com/questions/850341/how-do-i-work-around-javascripts-parseint-octal-behavior) when using `parseInt` without specifying the radix parameter – James Thorpe Dec 08 '14 at 12:31

1 Answers1

0

This is normal JS work. At JS exists reference between Objects (object, array). That's why you must make recursive copy of object (create new object/array), to prevent linking. In our case you can use "concat" function.

Example here: http://jsfiddle.net/0a46s334/

enter code here
sergolius
  • 428
  • 3
  • 8