I couldn't find here a solution here at Stackoverflow, in other case I am really sorry about duplicating similar problems.
Let's say, there is an string array:
let array = ["a", "b", "c", "d", "e", "f", "g"];
and some segments to be replaced:
let toBeReplaced = [
{s: 1, e: 2, new: ["b1", "b2", "c1", "c2", "c3"]},
{s: 3, e: 5, new: ["d1", "e1", "e2", "f1"]},
{s: 6, e: 0, new: ["g1", "g2", "a1"]}
];
The tricky things that these segments could be like third one, starting at the end of array and having last .e index at the beginning.
The output could be ["b1", "b2", "c1", "c2", "c3", "d1", "e1", "e2", "f1", "g1", "g2", "a1"]
, but it's much preferably to be like ["a1", "b1", "b2", "c1", "c2", "c3", "d1", "e1", "e2", "f1", "g1", "g2"]
.
If the task wouldn't have segments like third one:
let toBeReplaced = [
{s: 1, e: 2, new: ["b1", "b2", "c1", "c2", "c3"]},
{s: 3, e: 5, new: ["d1", "e1", "e2", "f1"]}
];