I am trying to remove elements from arrays while remembering their position and add them back later. So far I have this piece of code:
var my_array = ['A', 'B', 'C', 'D', 'E'];
var removed_elements = [];
// Assuming letter provided exists in my_array
function remove_element(letter) {
for (var index in my_array) {
if (my_array[index] == letter) {
break;
}
}
var removed_element = {
index: index,
letter: letter
}
removed_elements.push(removed_element);
my_array.splice(index,1);
}
// Assuming letter provided exists in removed_elements
function add_element(letter) {
for (var index in removed_elements) {
console.log('test');
if (removed_elements[index].letter == letter) {
console.log(removed_elements[index]);
break;
}
}
my_array.splice(removed_elements[index].index,0,removed_elements[index].letter);
}
It works fine as long as I remove 1 element at a time and add it back before removing another one. However, when I start removing several elements consecutively, the index saved for removed elements (not first one, but subsequent ones) becomes relative to the state of my_array at the time of removal, and not absolute to my_array‘s initial state, which can cause problems.
For instance if you remove 'B' and 'D' and add 'D' and 'B', you end up with ['A', 'B', 'C', 'E', 'D'] instead of ['A', 'B', 'C', 'D', 'E'].
Here is a jsfiddle showing what the problem is
What modifications should I change for my_array to end up in its initial state no matter of many elements I remove or add and how?
I thought about storing information about which elements surround the removed element at the time of removal and use that as extra info when adding back but wondering if there is a better way.
I wouldn’t actually remove them at all: