T dhanunjay
T dhanunjay

Reputation: 820

Issue when trying to splice an element from array

const data = response.data
console.log(data) let tmp = data.groups_with_selected[7]; 
data.groups_with_selected.splice(7, 2);
data.groups_with_selected.splice(2, 0, tmp);

I am trying to splice an element from an array from 7th position, and then insert into 2nd position. But during that process I am successfully able to change the 7th position to 2nd. but issue is after changing the position 7 or 8th position is reflecting in the array.

Upvotes: 0

Views: 60

Answers (1)

Ebay
Ebay

Reputation: 371

you can do it like that: you should choose the first element of the array which produced by splice by default to become an item and not an array, then insert it to your array at a certain position;

const temp = data.groups_with_selected.splice(7,1)[0]
 data.groups_with_selected.splice(2,0,temp )

so it will insert the 8th element in the third position;

Upvotes: 1

Related Questions