js code for flattening nested arrays