I have two array: array1 and array2. So array1 will be splitted based on the element insided array2. For example:
array1["1","2","3","4","5","6"]
array2["2","5"]
My code:
var prev = 0;
newArray = [];
for (var ii in array2) {
var index = array1.indexOf(array2[ii]);
if (index != prev) {
newArray.push(array1.slice(prev, index));
prev = index;
}
}
newArray.push(array1.slice(prev));
The result will be :
["1"],["2","3","4"],["5","6"]
But now i facing the problem of array1's element can be not in order. For example:["1","5","3","4","2","6"]. So based on the code i have, it will split the array1 wrongly because first element in array2 is "2", so it already split the array1 into two ["1","5","3","4"],["2","6"]. And next when come to "5", it cannot find it.
The expected result is:["1"],["5","3","4"],["2","6"]
So how to split array1 based on array2 no matter array1 in ascending ,descending or random order. Sorry my english is not good. Hope you guys can understand.
array2unique?"2"sinarray1: should they all be split points? Or just the first occurrence?