Considering I'm having the array:
var arr = [
{type:'a', name:'a1'}, {type:'b', name:'b1'}, {type:'hr'},
{type:'a', name:'a2'}, {type:'b', name:'b2'}, {type:'hr'},
{type:'a', name:'a3'}, {type:'b', name:'b2'}, {type:'hr'}
]
I'd split it into array of arrays of objects using {type:'hr'} object as separator element. So the result is:
[
[{type:'a', name:'a1'}, {type:'b', name:'b1'}],
[{type:'a', name:'a3'}, {type:'b', name:'b2'}],
[{type:'a', name:'a3'}, {type:'b', name:'b3'}]
]
Think lodash is useful for it?
Currently I've used _.map for this:
var result = [], sub = []; _.map(tokens, (it, idx) => { if(it.type === 'hr'){
result.push(sub);
sub = [];
} else {
sub.push(it);
}
});
if(sub.length){
result.push(sub);
}
console.log(result);
"aaxbbxcc"and you split onx, you'll get["aa", "bb", "cc"]whereas for your use case given an array like[ "a", "a", "x", "b", "b", "x", "c", "c"]and splitting on"x"should produce[ ["a", "a"], ["b", "b"], ["c", "c"] ]. Is that correct?