I have a collection
col1
var col1 = [1, 8, 9, 10, 2, 54, 7];
I have another collection
col2
var col2 = [1, 8, 23, 9, 46, 10, 2, 54, 78, 7]
...that I know it contains every element of
col1
, plus some other elementsI want to obtain this :
var col3 = [ [1, 8], [9], [10, 2, 54], [7] ]
col1
"splitted", "segmentized" bycol2
I'd like to use lodash functions for conciseness, but if someone brings an answer as vanilla JS, I'll accept the answer, and traduct it to lodash as an edit.
NB: I use ints for readability on SO, but my real collection contains objects.
NB2: each element is unique in both collections
NB3: elements that are common to both collections are in the same order
I think a plain Javascript algorithm will be shorter, faster, and easier to understand here:
Same thing, less concise, using a lodash loop:
I can't think of a proper pure lodash way - there just is no appropriate method. But we could write one ourselves:
and then use it like