Two objects are available:
ej= {
name="",
code: "",
namebusinessG:"",
codebusinessG:""
};
group = {
name:"",
code:""
}
Both of these objects will be stored in two arrays:
groupList:any[]=[];
ejList:any[]=[];
The program flow should follow these rules:
When a group is added to groupList: Check if the group already exists in groupList, if not, add it.
When an ej is added to ejList: Check if the ej already exists in ejList, if it does not exist, add it. Then, the program should add ej.codebusinessG and ej.namebusinessG to groupList. However, it must first verify beforehand if ej.codebusinessG already exists in the list of groupList.code, and only then add it.
selectItem(event) { if (!this.toggle) { // in group if (this.groupList.length == 0) { this.groupList.push(event); } else if (this.testNotExistinginArray(event, this.groupList)) { this.groupList.push(event); } } else { //in ej if (this.ejList.length == 0) { this.ejList.push(event); if (this.groupList.length == 0) { this.groupList.push({ code: event.codebusinessG, name: event.nameBusinessG }); } } else if (this.testNotExistinginArray(event, this.ejList)) { this.ejList.push(event); this.group.code = event.codeBusinessG; this.group.name = event.nameBusinessG; if (this.testNotExistinginArray(this.group, this.groupList)) { this.groupList.push(this.group); } } } }
A function is provided for testing whether a code exists in an array or not:
testNotExistinginArray(event,type) {
let notExist=true;
type.forEach (function(elm) {
if(elm.code === event.code) {
notExist=false
}
})
return notExist;
}
Current behavior
Adding Group: verification + addition OK
Adding ej: verification + addition OK
However, after adding the third ej, the corresponding group overwrites the last item in groupList instead of being added after it.
Here's a breakdown of what happens:
First ej is added (both ej and group are successfully added)
https://i.sstatic.net/Rrw1H.png
Second ej is added (again, both ej and group successfully added)
https://i.sstatic.net/l0HaE.png
Finally, the third ej is added...
https://i.sstatic.net/gLdBe.png
...but the codebusinessG overrides the last group in groupList instead of being added after it.
Expected behavior
When adding an ej
, the ej.codebusinessG + ej.namebusinessG should be added after the last item in groupList.
For context, I am using Angular 5 and the Primeng autocomplete component:
<p-autoComplete [(ngModel)]="text" [suggestions]="results" (completeMethod)="search($event)"
emptyMessage={{noBorrowerResult}}
[minLength]="3"
placeholder="Example: Apple"
[size] = "40"
field = "name"
[multiple]="true"
>
<ng-template let-elm pTemplate="item">
<div class="suggestion-item">{{elm.name}} ( ID: {{elm.code}} )</div>
</ng-template>
</p-autoComplete>