In my project i want to compare two object (images) of array and if the object value is same it should drop the object (images) and clear the previous one. but in my code if i drop the object all the previous objects are cleared.i just want to clear the same object only.
note: i need to implement this for captcha
file.ts:
origin = [{
img: 'https://www.gstatic.com/webp/gallery3/1.png'
},
{
img: 'https://www.gstatic.com/webp/gallery3/2.png'
},
{
img: 'https://www.gstatic.com/webp/gallery3/3.png'
}];
//---------------
samp = [{
img: 'https://www.gstatic.com/webp/gallery3/1.png'
},
{
img: 'https://www.gstatic.com/webp/gallery3/2.png'
},
{
img: 'https://www.gstatic.com/webp/gallery3/3.png'
}];
//---------------
destination = [
];
//---------------
drop(event: CdkDragDrop<string[]>) {
if (event.previousContainer === event.container) {
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
} else {
let item:any = event.previousContainer.data[event.previousIndex];
let copy:any = JSON.parse(JSON.stringify(item));
let element:any = {};
for(let attr in copy){
if(attr == 'title'){
element[attr] = copy[attr] += ' copy';
}else{
element[attr] = copy[attr];
}
}
//compare and drag
for (var i = 0, len = this.origin.length; i < len; i++) {
for (var j = 0, len2 = this.samp.length; j < len2; j++) {
if (this.origin[i].img === this.samp[j].img) {
this.samp.splice(j,1);
len2=this.samp.length;
this.destination=[element];
}
}
}
}
}
file.html:
<div class="fields-container">
<h2>Origin</h2>
<div
cdkDropList
#originList="cdkDropList"
[cdkDropListData]="origin"
[cdkDropListConnectedTo]="[destinationList]"
class="field-list">
<div class="field-box" *ngFor="let item of origin" cdkDrag>
<img src="{{item.img}}">
<div *cdkDragPlaceholder></div>
</div>
</div>
</div>
<div class="fields-container">
<h2>Copy</h2>
<div
cdkDropList
#destinationList="cdkDropList"
[cdkDropListData]="destination"
class="field-list"
(cdkDropListDropped)="drop($event)">
<!--dragged image-->
<div class="field-box" *ngFor="let item of destination" cdkDrag>
<img src="{{item.img}}">
</div>
<!--dragged image-->
<!--default image-->
<div class="field-box-samp" *ngFor="let images of samp" cdkDrag>
<img src="{{images.img}}">
</div>
<!--default image-->
</div>
</div>
output: