Reputation: 8061
Why does this array get 7 copies of data?
$('body').on('click', '#BtnSaveMed', function() {
console.log("Clicked");
var Prescription = DBrand = DGeneric = DDose = DDoseUnits = DFreq = DDurn = DDurUnits = [];
var loop = 0;
$(".DrugRow").each(function() {
console.log("Looped:" + loop++);
id = $(this).attr('id');
id = id.replace("DrugRow", "");
console.log("id:" + id);
BrandVal = $("#brand" + id).val();
GenericVal = $("#generic" + id).val();
DoseVal = $("#dose" + id).val();
DoseunitVal = $("#doseunits" + id).val();
FreqVal = $("#freq" + id).val();
DurnVal = $("#durn" + id).val();
DurnunitVal = $("#durnunit" + id).val();
console.log("Brand:" + BrandVal + " Generic" + GenericVal);
count = 0;
if ( (BrandVal != "") && (GenericVal != "")) {
console.log("if loop count:" + count++);
DBrand.push(BrandVal);
DGeneric.push(GenericVal);
DDose.push(DoseVal);
DDoseUnits.push(DoseunitVal);
DFreq.push(FreqVal);
DDurn.push(DurnVal);
DDurUnits.push(DurnunitVal);
}
});
var jsondata=JSON.stringify(DBrand);
console.log("DBrand is " + jsondata);
Prescription = [DBrand, DGeneric, DDose, DDoseUnits, DFreq, DDurn, DDurUnits];
var jsondata=JSON.stringify(Prescription);
console.log(jsondata);
});
HTML:
<div class="container">
<div class="row">
<div class="col-sm-6">
<span for="" class="text-left">Brand Name</span>
</div>
<div class="col-sm-6">
<span for="">Generic name</span>
</div>
<div class="col-sm-2">
<span for="">Dose</span>
</div>
<div class="col-sm-2">
<span for="">Units</span>
</div>
<div class="col-sm-3 col-sm-offset-5">
<span class="text-nowrap" for="">Frequency</span>
</div>
<div class="col-sm-1">
<span for="">Durn</span>
</div>
<div class="col-sm-2">
<span for="inputEmail4">Units</span>
</div>
</div>
<div class="row DrugRow" id="DrugRow1">
<div class="col-sm-6">
<input type="text" id="brand1" class="form-control BrandName input-sm ui-autocomplete-input" placeholder="Brand name" aria-label="BrandName" aria-describedby="" autocomplete="off">
</div>
<div class="col-sm-6">
<input type="text" id="generic1" class="form-control GenericName input-sm" placeholder="Generic name" aria-label="GenericName" aria-describedby="">
</div>
<div class="col-sm-2">
<input type="text" id="dose1" class="form-control Dose input-sm" placeholder="Dose" aria-label="Dose" aria-describedby="">
</div>
<div class="col-sm-2">
<input type="text" id="doseunits1" class="form-control DoseUnits input-sm" placeholder="mg" aria-label="DoseUnits" aria-describedby="">
</div>
<div class="col-sm-3">
<input type="text" id="freq1" class="form-control Frequency input-sm" placeholder="OD" aria-label="Frequency" aria-describedby="">
</div>
<div class="col-sm-2">
<input type="text" id="durn1" class="form-control Durn input-sm" placeholder="5" aria-label="Durn" aria-describedby="">
</div>
<div class="col-sm-2">
<input type="text" id="durnunit1" class="form-control DurationUnits input-sm" placeholder="days" aria-label="DurationUnits" aria-describedby="">
</div>
<div class="col-sm-1">
<a id="DelRow1" class="btn btn-danger btn-large DelRow" href="#" tabindex="5"><i class="fa fa-minus-circle fa-lg"></i></a>
</div>
</div>
<div class="row">
<div class="col-sm-23"></div>
<div class="col-sm-1">
<a id="AddRow2" class="btn btn-primary btn-large AddRow" href="#" tabindex="5"><i class="fa fa-plus-circle fa-lg"></i></a>
</div>
</div>
</div>
Console output:
Clicked
Looped:0
id:1
Brand:AUGMENTIN DUO 375MG TABLET GenericAMOXICILLIN 250MG+CLAVULANIC 125MG
if loop count:0
DBrand is ["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""]
[["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""]]
Expected data in Prescription:
["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""]
Data in Prescription:
[["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""],["AUGMENTIN DUO 375MG TABLET","AMOXICILLIN 250MG+CLAVULANIC 125MG","","","","",""]]
Why does my array have seven copies of what I was expecting?
Upvotes: 0
Views: 27
Reputation: 116
var Prescription = DBrand = DGeneric = DDose = DDoseUnits = DFreq = DDurn = DDurUnits = []; The above array type variables are reference type. That's why there is duplicate record. You can change the way of defining the variable like below: var Prescription = []; var DBrand =[]; var DGeneric =[]; Var DDose = []; var DDoseUnits = []; var DFreq =[]; var DDurn =[]; var DDurUnits = [];
Upvotes: 1