I faced a very interesting issue today which many of you might find elementary but since I am just learning to use jQuery, I am interested to know how this works.
I have two arrays and I'm iterating through the elements of array. One array is arrAllDetailsConstantData
and the other one is arrAllDetails
. I want to compare using arrAllDetailsConstantData and update using arrAllDetails.
I am using nested loops. But what is happening is that while updating array arrAllDetails, array arrAllDetailsConstantData is also getting updated. I assume this is somehow related to scope of parent this (though I am just guessing). Can you please help me with this?
Here is my code:
$.each(privateVariables.arrAllDetailsConstantData, function() {
if (this.AssociationId == value && this.uniqueChargeAttr == uniqueChargeAttr) {
if (this.Units == $("#txtUnits").val() &&
this.Modifier1 == $("#txtModifier1").val() &&
this.Modifier2 == $("#txtModifier2").val() &&
this.Modifier3 == $("#txtModifier3").val() &&
this.Modifier4 == $("#txtModifier4").val() &&
this.DxOption == $("#ddlDxOption").val() &&
this.DxCode1 == $("#txtDx1").val() &&
this.DxCode2 == $("#txtDx2").val() &&
this.DxCode3 == $("#txtDx3").val() &&
this.DxCode4 == $("#txtDx4").val()) {
} else {
$.each(arrAllDetails, function() {
if (this.AssociationId == value && this.uniqueChargeAttr == uniqueChargeAttr) {
this.ActionType = "M";
this.CptName = $("#lblCptDesc").text();
this.CptDesc = $("#lblCptDesc").text();
this.Units = $("#txtUnits").val();
this.Modifier1 = $("#txtModifier1").val();
this.Modifier2 = $("#txtModifier2").val();
this.Modifier3 = $("#txtModifier3").val();
this.Modifier4 = $("#txtModifier4").val();
this.DxOption = $("#ddlDxOption").val();
this.DxCode1 = $("#txtDx1").val();
this.DxCode2 = $("#txtDx2").val();
this.DxCode3 = $("#txtDx3").val();
this.DxCode4 = $("#txtDx4").val();
privateVariables.arrActionData.push(this);
}
});
}
}
});
// test code ends