• Home
  • Jobs
  • Courses
  • Questions
  • Teachers
  • For business
  • ES/EN

0

20
Views
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.

about 1 month ago ·

Juan Pablo Isaza

1 answers
Answer question

0

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;

about 1 month ago · Juan Pablo Isaza Report
Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.