简体   繁体   English

尝试从数组拼接元素时出现问题

[英]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.我试图从第 7 个位置的数组中拼接一个元素,然后插入到第 2 个位置。 But during that process I am successfully able to change the 7th position to 2nd.但在这个过程中,我成功地将第 7 位更改为第 2 位。 but issue is after changing the position 7 or 8th position is reflecting in the array.但问题是在改变位置后第 7 或第 8 个位置反映在阵列中。

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;您可以这样做:您应该选择默认情况下由 splice 生成的数组的第一个元素成为项目而不是数组,然后将其插入到您的数组的某个位置;

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;所以它会在第三个位置插入第 8 个元素;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM