I have this text
( ) ( ( ) ( ( ) ) ( ( ) ) )
Pratically each bracket is a span and i have writed this code to organize the nested structure:
myobj.each(function(i) {
var isOpen = $( this ).text( ) == '(' ? true: false;
if( isOpen ) {
console.log( ' open at: ' + index )
index++;
closer = index;
} else {
closer--;
console.log( 'close at: ' + ( closer ) );
}
});
Assuming that the expeted output would be:
0 0 1 2 2 3 4 4 3 5 6 6 5 1
( ) ( ( ) ( ( ) ) ( ( ) ) )
it give me:
0 0 1 2 2 3 4 4 3 5 6 6 5 4
( ) ( ( ) ( ( ) ) ( ( ) ) )
note the last bracket.. Why?
Store the unclosed (just opened) bracket’s index in an array, then use
popto get the last unclosed one when closing.Fiddle
The
logabove is just for showing the final result, those 3 lines + declaration can later be removed.