So I have two array like this.
var array1 = ["str1", "str2", "str3", "str4", "str5", "str6"];
How can i split the array1 based on elements in array2 so that the result become like this and store in 3 array(not always 3,depends on array2's items)?
var array2 = ["str2","str5"];
// Case1 result
// newArr1=["str1"]
// newArr2=["str2","str3","str4"]
// newArr3=["str5","str6"]
var array2 = ["str2","str3"];
//Case2 result
// newArr1=["str1"]
// newArr2=["str2"]
// newArr3=["str3","str4""str5","str6"]
I tried this and i have no idea how to achieve result like above.
for (i = 0; i < array2.length; i++) {
newArr = array1.splice(0, array1.indexOf(array2[i]));
}
array2 acts as a breaking point. So if array2 contain all elements of array1. It should generate 6 new array with only one item in each.
[["str1"],["str2","str3","str4"],["str5","str6"]]?["str2","str5"]str2andstr5are the breaking points inarray1