collect funtion to return nested element as an array