2014-06-24 36 views
0

我有這個JSON負載,我想在幾個地方使用它,並想知道如何從這個拒絕兩個鍵。如何在jQuery中拒絕JSON密鑰?

@metaTagsAdvanced = { 
    App: { comparison: ['was', 'was not'], value: ['Opened in the last two days', 'Opened in the last two weeks', 'Opened in the last month'], enabled: true }, 
    AppVersion: { comparison: ['equals', 'not equal', 'greater than', 'less than', 'greater than or equal', 'less than or equal'], value: 'string', enabled: true }, 
    ControlGroup: { comparison: ['less than', 'less than or equal', 'greater than', 'greater than or equal', 'equals'], value: 'number', numberOptions: {min: 1, max: 10}, enabled: true }, 
    Country: { comparison: ['is', 'is not'], value: 'country', enabled: true }, 
    Deliverable: { comparison: ['is', 'is not'], value: ['Push Notification','Local Push Notification', 'App Originated Push', 'In-App Alert', 'In-App Content', 'SMS', 'MMS', 'Email', 'Rich Message'], enabled: true }, 
    Event: {comparison: {eventNumber: ['did occur N days ago', 'did occur greater than N days ago','did occur greater than or equal to N days ago','did occur less than N days ago','did occur less than or equal to N days ago', 'did not occur N days ago','did not occur greater than N days ago','did not occur greater than or equal to N days ago','did not occur less than N days ago','did not occur less than or equal to N days ago'], standards: ['did occur', 'did not occur']}, value: 'events', enabled: true}, 
    InstallDate: { comparison: ['before', 'was', 'after', 'within', 'days ago', 'greater than N days ago', 'greater than or equal to N days ago','less than N days ago', 'less than or equal to N days ago'], value: 'date', enabled: true }, 
    Language: { comparison: ['is', 'is not'], value: 'language', enabled: true }, 
    LastOpenDate: { comparison: ['before', 'was', 'after', 'within', 'N days ago', 'greater than N days ago', 'greater than or equal to N days ago', 'less than N days ago', 'less than or equal to N days ago'], value: 'date', enabled: true }, 
    OS: { comparison: ['is', 'is not'], value: ['android', 'ios'], enabled: true }, 
    PushOpenRate: { comparison: ['greater than or equal', 'less than or equal'], value: ['0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1'], enabled: true }, 
    Segment: {comparison: ['is in'], value: 'segments', enabled: true}, 
    Sessions: { comparison: ['less than', 'greater than'], value: 'number', numberOptions: {min: 1}, enabled: true }, 
    Tag: {comparison: {string: ['is', 'is not', 'contains'], double: ['equals', 'not equal to', 'less than', 'greater than', 'less than or equal', 'greater than or equal'], timestamp: ['before', 'after', 'was', 'within', 'N days ago', 'greater than N days ago', 'greater than or equal to N days ago','less than N days ago','less than or equal to N days ago'], segment: ['is in', 'is not in'], standard: ['exists', 'does not exist']}, value: 'tags', enabled: true}, 
    Timezone: {comparison: ['is', 'is not'], value: 'timezone', enabled: true} 
} 

所以我想插入實例,如下所示。但是,我怎麼能改變這段代碼來拒絕/排除上面的'Segment'和'Timezone'鍵?

filters = $.extend({}, @metaTagsAdvanced) 

任何與此有關的幫助將不勝感激,因爲它可以讓我顯着重構我的一些代碼! (我用的CoffeeScript,因此爲什麼它的格式事情是這樣的!)

乾杯

+0

爲什麼不簡單和刪除不需要的密鑰? O_O –

+0

包含行'filters = $ .extend({},@metaTagsAdvanced)''的文件的文件名是什麼? – 7stud

+0

它們都在同一個文件中,'filters = $ .extend({},@metaTagsAdvanced)'就在文件的後面。 – iamryandrake

回答

1

我會使用一個實用程序庫像lodash或強調,會比這個解決方案的更多強大的,但你可以做像這樣的東西,如果你不想依賴Array上的原生原型方法。間歇性日誌語句顯示它如何建立。

的jsfiddle這裏:http://jsfiddle.net/TRqG8/

jQuery的只有

someTags = 
    Foo: {foo: 'bar'} 
    Bar: {foo: 'bar'} 
    Baz: {foo: 'bar'} 

_keys = (obj)-> $.map(obj, (value, key)-> key) # seems odd that key is last but that appears to be the way jQuery 2.1.0 works. 

console.log _keys(someTags) # => ["Foo", "Bar", "Baz"] 

_in = (arr, key)-> arr.indexOf(key) != -1 

console.log _in(_keys(someTags), 'Foo') #=> true 

# there is a native filter function for Array.prototype 
_filter = (obj, keys...)-> 
    objKeys = _keys(obj) 
    # a poor man's reduce 
    ob = {} 
    for k in objKeys 
    ob[k] = obj[k] unless _in(keys, k) 
    ob 

console.log _filter(someTags, 'Bar') #=> Object {Foo: Object, Baz: Object} 

console.log $.extend({}, _filter(someTags, 'Bar', 'Baz')) #=> Object {Foo: Object} 

下劃線

相比之下,使用下劃線,你可以做這樣的事情:

someTags = 
    Foo: {foo: 'bar'} 
    Bar: {foo: 'bar'} 
    Baz: {foo: 'bar'} 

console.log _.extend({}, _.omit(someTags, 'Bar')) 

的jsfiddle:http://jsfiddle.net/nUSM8/1/

+0

假設您正在計劃使用'extend',我已經使用'extend'顯示了。如果你只是想過濾,你可以忽略擴展的調用。 –

+0

傑德,這正是我正在尋找的,我已經在我的項目中使用了Underscore,所以這非常有幫助! – iamryandrake

+0

這是相當有趣的工作!思考的食物:爲構造函數使用大寫字母(你使用'new'創建的那些字符,比如'new Object()')。即使你可以把它們當作鑰匙,你也可以通過避免語法陷阱來節省一些頭痛的問題。 –