2016-06-09 65 views
0

我這裏有這樣一串代碼:軌忽略添加到陣列

doc.xpath("//script[@type='text/javascript']/text()").each do |text| 
     if text.content =~ /more_options_on_polling/ 
     price1 = text.to_s.scan(/\"(formatted_total_price)\":\"(.+?)\"/).uniq 
     description = text.to_s.scan(/\"(ticket_desc)\":\"(.+?)\"/).uniq 
     price = price1 + description 
     testing = price1.map{|a| { a[0] => a[1] } } 
     desc = description.map{|a| { a[0] => a[1] } } 
     respond_to do |format| 
      format.json { render :json => {:testing => testing, 
              :desc => desc }} 
     end 
     end 
在遞減

現在,它返回:

"desc": [ 
    { 
     "ticket_desc": "Later Owl Ticket" 
    }, 
    { 
     "ticket_desc": "Later Owl Ticket+Collector Ticket &#64 extra £4.95 per ticket" 
    }, 
    { 
     "ticket_desc": "Later Owl + Chance For VIP Upgrade" 
    }, 
    { 
     "ticket_desc": "VIP Ticket" 
    }, 
    { 
     "ticket_desc": "VIP Ticket + Collector Ticket &#64 extra £4.95 per ticket" 
    }, 
    { 
     "ticket_desc": "Skydeck Package" 
    }, 
    { 
     "ticket_desc": "5 Person Skydeck Table" 
    }, 
    { 
     "ticket_desc": "7 Person Skydeck Table" 
    }, 
    { 
     "ticket_desc": "10 Person Skydeck Table" 
    } 
    ] 

什麼IM希望是內部一個+號什麼被忽略。因此,最好的迴應是這樣的:

"desc": [ 
    { 
     "ticket_desc": "Later Owl Ticket" 
    }, 
    { 
     "ticket_desc": "VIP Ticket" 
    }, 
    { 
     "ticket_desc": "Skydeck Package" 
    }, 
    { 
     "ticket_desc": "5 Person Skydeck Table" 
    }, 
    { 
     "ticket_desc": "7 Person Skydeck Table" 
    }, 
    { 
     "ticket_desc": "10 Person Skydeck Table" 
    } 
    ] 

我目前正在做這跟軌,但我renderign成通過JavaScript/AJAX的觀點,所以,如果你有任何想法,我能做些什麼讓有我知道:)

繼承人的AJAX:

for (var i = 0; i < json.testing.length; i++) { 
    var section = json.testing[i].formatted_total_price; 
    var desc = json.desc[i].ticket_desc; 

回答

0

那麼,一個選擇是在desc哈希使用選擇。例如,與desc =開頭的行後,您可以添加:

desc.select! { |a| !a['ticket_desc'].include?("+") } 

如果你想保持原來的遞減,使用非破壞性的選擇,並把結果在一個新的變量:

desc_wo_plus = desc.select { |a| !a['ticket_desc'].include?("+") } 
+0

嘿!我得到了'NoMethodError(未定義的方法'include?'for nil:NilClass):'這是因爲我把它放在每個 –

+0

對不起,我認爲你的數據是一個散列,但實際上是一個散列數組。我已經更新了我的答案。 – apod

+0

謝謝!這工作第一次! –