Skip to content Skip to sidebar Skip to footer

Moving Element From One Array Into Another

I've tried to make the following Stackblitz which use ng2-dragula. My problem laid in the moveback() and moveto() function which are supposed to move from one array into another wh

Solution 1:

Update your for condition as below.

for (let i = target.length - 1; i >= 0; i--) {...}

The issue with your logic is, If you select 2nd & 3rd element and apply moveto then for 2nd element it will work fine. But then in your actual this.target array will be changed. Now 3rd element will become 2nd because of your line this.target.splice(i, 1);. So when you move 3rd element in for loop iteration, it will actually move 4th one.

Check with Updated fiddle Here

Post a Comment for "Moving Element From One Array Into Another"