Почему этот массив получает 7 копий данных?
$('body').on('click', '#BtnSaveMed', function() {
console.info("Clicked");
var Prescription = DBrand = DGeneric = DDose = DDoseUnits = DFreq = DDurn = DDurUnits = [];
var loop = 0;
$(".DrugRow").each(function() {
console.info("Looped:" + loop++);
id = $(this).attr('id');
id = id.replace("DrugRow", "");
console.info("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.info("Brand:" + BrandVal + " Generic" + GenericVal);
count = 0;
if ( (BrandVal != "") && (GenericVal != "")) {
console.info("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.info("DBrand is " + jsondata);
Prescription = [DBrand, DGeneric, DDose, DDoseUnits, DFreq, DDurn, DDurUnits];
var jsondata=JSON.stringify(Prescription);
console.info(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>
Вывод в консоль:
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","","","","",""]]
Ожидаемые данные в рецепте:
["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","","","","",""]]
Почему в моем массиве семь копий того, что я ожидал?



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


var Prescription = DBrand = DGeneric = DDose = DDoseUnits = DFreq = DDurn = DDurUnits = []; Перечисленные выше переменные типа массива относятся к ссылочному типу. Вот почему есть повторяющаяся запись. Вы можете изменить способ определения переменной, как показано ниже: var Prescription = []; вар DBrand = []; var DGeneric = []; Var DDose = []; var DDoseUnits = []; var DFreq = []; var DDurn = []; var DDurUnits = [];