0

Please have a look at the code below. The problem I'm having is that it seems that I create 4 references to the same object rather than 4 unique objects because when I change one value in groupDataArrays the same value is also changes in groupDataArraysOfficial, groupDataArraysValid, groupDataArraysOfficialValid

I'm trying to do a jQuery extend in order to avoid creating a reference, for example

groupDataArrays[resultKey] = jQuery.extend({}, resultGroup);

But it don't work. Do you know how to avoid this referencing error?

Thanks in advance.

    for (var i = 0; i < dataCollection.length; i++) {
            var data = dataCollection[i];
            var currentRecord = data.Record.Record;
            for (var resultKey in currentRecord) {

                    //It seems like this declaration hase something to do with the referencing
                    var resultGroup = currentRecord[resultKey];
                    var resultGroupOfficial = currentRecord[resultKey];
                    var resultGroupValid = currentRecord[resultKey];
                    var resultGroupOfficialValid = currentRecord[resultKey];


     //Every element contains these objects so its enough to loop through the first 
    //and then fix the other values at the same time. Perhaps this is the error?
   //But it seems uneccesary to do 4 for loops instead of doing one?
                    for (var resultGroupElement in resultGroup) {
                        if (dataTables[resultGroupElement] != null) {
                            var curResultGroup = dataTables[resultGroupElement];
                        }
                        else {
                            var curResultGroup = [];
                        }
                        var stopTimeConverted = convertDateToWorkWithAllBrowsers(data.StopTime); //Is necesary in order for date objects to be created in firefox and I.E

                        if (currentRecord[resultKey][resultGroupElement] != null
                                && currentRecord[resultKey][resultGroupElement] !== undefined
                                && currentRecord[resultKey][resultGroupElement] !== ""
                                && currentRecord[resultKey][resultGroupElement] !== "N/A") {

                            curResultGroup.push(
                                    [
                                        new Date(stopTimeConverted),
                                        parseInt(currentRecord[resultKey][resultGroupElement]),
                                        resultKey,
                                        data

                                    ]
                                    );
                            resultGroup[resultGroupElement] = curResultGroup;
                            dataTables[resultGroupElement] = curResultGroup;
                            /***************************************************/
                            /***************Create Only Official Graph**********/
                            /***************************************************/                            
                            if (data.Official == "True" || data.Official === true) {

                                if (dataTablesOfficial[resultKey] != null) {
                                    var dtOfficial = dataTablesOfficial[resultKey];
                                } else {
                                    var dtOfficial = [];
                                }
                                dtOfficial.push(
                                        [
                                            new Date(stopTimeConverted), 
                                            parseInt(currentRecord[resultKey][resultGroupElement]),
                                            resultKey,
                                            data
                                        ]
                                        );
                                resultGroupOfficial[resultGroupElement] = dtOfficial;
                                dataTablesOfficial[resultGroupElement] = dtOfficial;

                            }
                            /***************Create Only Valid Graph******************/
                            if (data.Valid == "True" || data.Valid === true) {
                                if (dataTablesValid[resultKey] != null) {
                                    var dtValid = dataTablesValid[resultKey];
                                } else {
                                    var dtValid = [];
                                }
                                dtValid.push(
                                        [
                                            new Date(stopTimeConverted), 
                                            parseInt(currentRecord[resultKey][resultGroupElement]),
                                            resultKey,
                                            data
                                        ]
                                        );
                                resultGroupValid[resultGroupElement] = dtValid;
                                dataTablesValid[resultGroupElement] = resultGroupValid;
                            }
                            /***************Create Official and Valid Graph******************/
                            if (data.Valid == "True" || data.Valid === true && data.Official == "True") {
                                if (dataTablesOfficialValid[resultKey] != null) {
                                    var dtValidOfficial = dataTablesOfficialValid[resultKey];
                                } else {
                                    var dtValidOfficial = [];
                                }
                                dtValidOfficial.push(
                                        [
                                            new Date(stopTimeConverted),
                                            parseInt(currentRecord[resultKey][resultGroupElement]),
                                            resultKey,
                                            data
                                        ]
                                        );
                                resultGroupOfficialValid[resultGroupElement] = dtValidOfficial;
                                dataTablesOfficialValid[resultGroupElement] = dtValidOfficial;
                            }
                        }
                    }
                    groupDataArrays[resultKey] = jQuery.extend({}, resultGroup);
                    groupDataArraysOfficial[resultKey] = jQuery.extend({}, resultGroupOfficial);
                    groupDataArraysValid[resultKey] = jQuery.extend({}, resultGroupValid);
                    groupDataArraysOfficialValid[resultKey] = jQuery.extend({}, resultGroupOfficialValid);
                }
    }
anders
  • 1,535
  • 5
  • 19
  • 43
  • possible duplicate of [Most elegant way to clone a JavaScript object](http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object) – Jean-Karim Bockstael Sep 09 '14 at 15:29

1 Answers1

0

Just pass true as the first argument to $.extend() in front of the {} - it'll perform a deep copy of the entire structure.

Alnitak
  • 334,560
  • 70
  • 407
  • 495