Hello I couldn’t figure out how to do this,
Let’s say I have an array like this
main = [ "1","2","A","4","5","B","6","7","A","8","9","B","10"];
I want to get a new array with result
main2 = ["A","4","5","B","A","8","9","B"]
and finally break them apart like the following;
main3 = ["A","4","5","B"]
main4 = ["A","8","9","B"]
As you can see I am taking out the array items from A-B that happened twice.
>Solution :
Here’s the old-school approach.
const main = ["1", "2", "A", "4", "5", "B", "6", "7", "A", "8", "9", "B", "10"];
const start = "A";
const end = "B";
let result = [];
let isCollecting = false;
main.forEach(item => {
if (item === start) isCollecting = true;
if (isCollecting) result.push(item);
if (item === end) {
console.log(result);
result = [];
isCollecting = false;
}
});