// splits an array at the first element `predicate(e)` returns true for
functionsplitAt(arr,predicate){
constindex=arr.findIndex(predicate);
if(index==-1)returnsplit(arr,arr.length);
returnsplit(arr,index);
}
nodes=[1,1,3,2,1,1,2,3,1,2];
// in this example the sequence [3, 2] starts a "section"
splitAt(nodes,(node,index,src)=>{
return(node==2)&&(src[index-1]==3);
});
otherNodes=[
'blockquote','p','p',
'h2','blockquote','p','p','p',
'h2','blockquote','p','p','p',
];
isTitleNode=(node)=>node=='h2';
functionsectionReducer(prev,cur){
// when we encounter a title, start a new array
if(isTitleNode(cur)){
prev.push([cur]);
}else{
// otherwise push the current node into the previous array
prev.at(-1).push(cur);
}
returnprev;
}
otherNodes.reduce(
sectionReducer,
[[]]// start with array containing an empty array since we're returning an array of arrays
);
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.