function add(list, newItems, toAdd) {
let result = [];
let from = 0;
let prevOmega = -1;
toAdd.forEach((omega, index )=> {
let to = from + (omega - prevOmega - 1)
result = [...result, ...list.slice(from , to), newItems[index]];
from = to;
prevOmega = omega;
});
result = [...result, ...list.slice(from)];
return result;
}
function remove(list, toRemove) {
let result = [];
let from = 0;
toRemove.forEach(to => {
result = [...result, ...list.slice(from, to)];
from = to + 1;
});
result = [...result, ...list.slice(from)];
return result;
}
/* A B C D => A C D
* remove item at index 1
*
* A B C D => A B C D E
* Add E at index 4
*
* A B C D => B D
* remove item at index 2 then remove item at index 0
*
* B D => A B C D
* Add item before item at index 0 then add item before item at index 2
*
* A B C D => A B Z D
* remove item at index 2
* A B C D => A B D
* Add item before index 2
* A B D => A B Z Djk
*/
let result = remove(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], [1, 3, 6]);
console.log(result);
result = add(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], ['X', 'Y'], [3, 7]);
// A X B Y C Z D
console.log(result);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.