【问题标题】:How do you reject a JSON key in jQuery?你如何拒绝 jQuery 中的 JSON 键?
【发布时间】:2014-08-15 08:48:04
【问题描述】:

我有这个 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,因此它的格式是这样的!)

干杯

【问题讨论】:

  • 为什么不简单的 for 和删除不需要的键呢? O_O
  • 包含filters = $.extend({}, @metaTagsAdvanced)行的文件的文件名是什么?
  • 都在同一个文件中,filters = $.extend({}, @metaTagsAdvanced) 在文件的更下方。

标签: jquery ruby json coffeescript


【解决方案1】:

我会使用 lodash 或 underscore 之类的实用程序库,它会比此解决方案更强大,但如果您不想依赖 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/

【讨论】:

  • 我已经展示了使用extend,假设您计划使用extend。如果您只想过滤,可以忽略对扩展的调用。
  • Jed,这正是我一直在寻找的,我已经在我的项目中使用了 Underscore,所以这非常有用!
  • 完成工作非常有趣!深思熟虑:对构造函数使用大写的单词(您将使用“new”创建的函数,例如new Object())。即使您可以将它们用作键,但通过避免语法陷阱,您可以省去一些麻烦。
  • 如何使用下划线扩展现有对象以添加一些标签? _.extend({}, _.something(Blah, someTags)
  • _.extend({Blah: {}}, _.omit(someTags, 'Bar')) 我想这就是你想要的。
猜你喜欢
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
相关资源
最近更新 更多