2011-10-18 269 views
0

我有以下json對象數組。json對象的過濾器數組

[{"template":{"title":"title 1"}},{"template":{"title":"title 1"}},{"template":{"title":"title1111"}},{"template":{"title":""}},{"template":{"title":""}},{"template":{"title":"this is the title"}},{"template":{"title":"title232"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"this is a title"}},{"template":{"title":"this is a title"}},{"template":{"title":"this is a title"}},{"template":{"title":"some title"}},{"template":{"title":"some title"}},{"template":{"title":"some title"}},{"template":{"title":"some title"}}] 

我想這陣能像

[{'title': 'some title'}, {'title': 'some other title'}, ...] 

所以基本上我不想讓JS響應裏面的模板屬性。 我是從下面的Rails代碼得到這個

@templates = Template.find_all_by_user_id(current_user.id, :select=>'title', :conditions=>"title is not null") 

@templates.to_json是什麼給了我這個數組JSON對象的

有什麼建議?

回答

0

我認爲,在這種情況下,你可以只收集Template是你的整個集合的屬性做:

@templates = Template.find_all_by_user_id(current_user.id, :select=>'title', :conditions=>"title is not null").collect(&:attributes) 

(滾動至看到添加的.collect(&:attributes)在該行的末尾權。)