I have a list which contains a team according to color:
var team = ["Red","Green","Blue","Yellow","Black","White","Orange"]
var from = 0
var to = 5
team.splice(to, 0, team.splice(from, 1)[0])
console.log(team)
Here I am chaning the index of the team from 0 to 5 which gives me output like:
["Green","Blue","Yellow","Black","White","Red","Orange"]
The 0 index positioned to 5.
Now I have an dictionary which contains the captain for the team according to index.
var captians = [{'index': 0, 'captain': 'Jack'}, {'index': 1, 'captain': 'Daniel'}]
Here 0 is the index of team.
Team red's captain is Jack
When team is changed in index I want to change the captains accordingly.
How can I do this ??
You could iterate over and move all indices in the interval a position.
var team = ["Red", "Green", "Blue", "Yellow", "Black", "White", "Orange"],
from = 0,
to = 5,
captians = [{ 'index': 0, 'captain': 'Jack' }, { 'index': 1, 'captain': 'Daniel' }];
team.splice(to, 0, team.splice(from, 1)[0]);
captians.forEach(function (a) {
if (a.index === from) {
a.index = to;
return;
}
if (from < to && a.index > from && a.index <= to) {
a.index--;
}
if (from > to && a.index >= to && a.index < from) {
a.index++;
}
});
console.log(captians);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With