compact过滤数组中的nil

时间:2023-03-08 21:57:32
compact过滤数组中的nil

http://ruby-doc.org/core-2.2.0/Array.html#method-i-compact

compact → new_aryclick to toggle source

Returns a copy of self with all nil elements removed.

[ "a", nil, "b", nil, "c", nil ].compact
#=> [ "a", "b", "c" ]
 render :json => {
:videos => result_videos.compact.map do |video|
if video.class == CmsTvIVideo
if video.video_type == "live"
{
:title => video.title,
:live_name => video.live_name,
:live_id=> video.live_id,
:big_horizontal_image => video.res_img_l,
:middle_horizontal_image => video.res_img_m,
:big_vertical_image => video.res_img_s,
:middle_vertical_image => video.middle_vertical_image,
:mtype => "live"
}