你如何拒绝jQuery中的JSON密钥?

时间:2014-06-24 22:54:41

标签: jquery ruby json coffeescript

我有这个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}
}

所以我想插入实例,如下所示。但是,如何更改此代码以拒绝/排除上方的“细分”和“时区”键?

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

对此的任何帮助都将非常感激,因为这将允许我显着重构我的一些代码! (我正在使用coffeescript,因此为什么它按照原样进行格式化!)

干杯

1 个答案:

答案 0 :(得分:1)

我使用像lodash或underscore这样的实用程序库比这个解决方案更强大,但是如果你不想依赖于Array上的原生原型方法,你可以做这样的事情。间歇性日志语句,以显示它是如何构建的。

jsfiddle here: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/