如何使用Javascript或jQuery过滤JSON数据?
这是我的JSON数据:
[{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}]
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
obj1 = JSON.parse(jsondata);
Run Code Online (Sandbox Code Playgroud)
现在我只想要包含网站的名称和网站数据等于"雅虎"
我需要input一个div没有表单的框,当用户输入内容并返回时,它应该运行Javascript函数.
没有提交按钮.
我怎样才能做到这一点?
每次我尝试在Windows中生成Rails控制器时,都会在生成必要文件后启动服务器。
$ rails g controller supprefresh index process
io/console not supported; tty will not be manipulated
C:/Rubies/jruby-1.7.9/lib/ruby/shared/Win32API.rb:11 warning: warning: variable $KCODE is no longer effective
create app/controllers/supprefresh_controller.rb
route get "supprefresh/process"
route get "supprefresh/index"
invoke erb
create app/views/supprefresh
create app/views/supprefresh/index.html.erb
create app/views/supprefresh/process.html.erb
invoke rspec
create spec/controllers/supprefresh_controller_spec.rb
create spec/views/supprefresh
create spec/views/supprefresh/index.html.erb_spec.rb
create spec/views/supprefresh/process.html.erb_spec.rb
invoke helper
create app/helpers/supprefresh_helper.rb
invoke rspec
create spec/helpers/supprefresh_helper_spec.rb
invoke assets
invoke coffee
create app/assets/javascripts/supprefresh.js.coffee
invoke scss
create app/assets/stylesheets/supprefresh.css.scss
[2019-04-09 16:18:34] INFO WEBrick 1.3.1
[2019-04-09 16:18:34] INFO ruby 2.0.0 (2013-12-06) …Run Code Online (Sandbox Code Playgroud) 这是红宝石中多级继承的示例,这里有3个类A,B和C。B继承自A,C继承自B,因此最后,类C具有A,B和C的所有方法。
class A
def hello_by_a
puts "A says hello"
end
end
class B < A
def hello_by_b
puts "B says hello"
end
end
class C < B
def hello_by_c
puts "C says hello"
end
end
c = C.new
c.hello_by_a #=> A says hello
c.hello_by_b #=> B says hello
c.hello_by_c #=> C says hello
p c.methods-Object.methods #=> [:hello_by_c, :hello_by_b, :hello_by_a]
Run Code Online (Sandbox Code Playgroud)
这和mixin一样,这里不是类A和B,而是包含了模块A和B,它们已包含在类C中。现在,类C具有所有3种方法
module A
def hello_by_a
puts "A says hello"
end
end
module B
def hello_by_b
puts "B says hello"
end
end …Run Code Online (Sandbox Code Playgroud) 请考虑下表:
SELECT id, Bill_Freq, Paid_From, Paid_To, Paid_Dt, rev_code FROM psr_20160708091408;
Run Code Online (Sandbox Code Playgroud)
要求是获取已rev_code填充字符串**SUM**的行.我还注意到,对于rev_code填充为**SUM**的每一行,它Bill_Freq都不会为null或零.
所以我写了两个查询来获取id最低的行
基于where子句中的字符串检查查询:
select
min(id) as head_id,
bill_freq,
Paid_From,
Paid_To,
Paid_Dt
from
`psr_20160708091408` where rev_code = "**SUM**";
Run Code Online (Sandbox Code Playgroud)
基于真实条件的查询:
select
min(id) as head_id,
bill_freq,
Paid_From,
Paid_To,
Paid_Dt
from
`psr_20160708091408` where bill_freq;
Run Code Online (Sandbox Code Playgroud)
我没有看到有人使用第二种类型,想知道它的可靠性和失败的情况.
我需要解析2d数组的以下哈希,其中第一个数组具有键,其余数组具有值。
input = {
"result": [
[
"id",
"name",
"address"
],
[
"1",
"Vishnu",
"abc"
],
[
"2",
"Arun",
"def"
],
[
"3",
"Arjun",
"ghi"
]
]
}
Run Code Online (Sandbox Code Playgroud)
这是我想出的结果。
input[:result].drop(1).collect{|arr| Hash[input[:result].first.zip arr]}
Run Code Online (Sandbox Code Playgroud)
在这里,我在遍历result数组时忽略了它的第一个子数组(包含键的那个),然后zip是键数组和值数组以进行哈希处理,然后将哈希收集到另一个数组中。
上面的解决方案给我我想要的是一个哈希
[{"id"=>"1", "name"=>"Vishnu", "address"=>"abc"}, {"id"=>"2", "name"=>"Arun", "address"=>"def"}, {"id"=>"3", "name"=>"Arjun", "address"=>"ghi"}]
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来达到相同的结果?
在ruby中,我们可以Object.methods获取特定Object或类的所有方法。
例如:
irb(main):001:0> Object.methods
=> [:new, :allocate, :superclass, :<=>, :include, :<=, :>=, :==, :===, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :public_constant, :private_constant, :deprecate_constant, :singleton_class?, :module_exec, :class_exec, :freeze, :inspect, :const_missing, :class_eval, :method_defined?, :public_method_defined?, :prepend, :<, :>, :private_method_defined?, :protected_method_defined?, :public_class_method, :module_eval, :to_s, :private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :remove_instance_variable, :instance_variable_set, :method, :public_method, :singleton_method, :extend, :define_singleton_method, :to_enum, :enum_for, :=~, :!~, :eql?, :respond_to?, :object_id, :send, :display, :nil?, …Run Code Online (Sandbox Code Playgroud) irb(main):007:0> %w[1 2 3 4 5]&.each { |a| puts a }
1
2
3
4
5
=> ["1", "2", "3", "4", "5"]
irb(main):008:0> %w[1 2 3 4 5].each { |a| puts a }
1
2
3
4
5
=> ["1", "2", "3", "4", "5"]
Run Code Online (Sandbox Code Playgroud)
双方&.each并.each似乎给了相同的结果
ruby-doc似乎没有关于此功能的任何内容
两者有什么区别?
我在 jruby irb 上玩,遇到了这种现象,其中参数方法[[:rest]]在字符串方法上调用时返回。这不仅是字符串的情况,而且我将举一个关于字符串的例子。
irb(main):042:0> String.new.methods-Object.methods
[:valid_encoding?, :casecmp, :to_java_bytes, :squeeze!,
:is_utf8?, :slice, :hex, :[]=, :initialize_copy, :empty?,
:oct, :rindex, :unseeded_hash, :%, :rjust, :chop, :index,
:gsub!, :chomp!, :*, :+, :concat, :capitalize, :singularize,
:titlecase, :each_line, :size, :deconstantize, :downcase!,
:capitalize!, :to_sym, :humanize, :setbyte, :force_encoding,
:sub, :reverse!, :swapcase, :scan, :gsub, :sum, :partition,
:to_str, :codepoints, :swapcase!, :byteslice, :end_with?,
:upto, :tr!, :[], :intern, :parameterize, :tableize, :chomp,
:pluralize, :reverse, :mb_chars, :succ, :underscore, :titleize,
:start_with?, :ljust, :tr, :chars, :chop!, :encode, :<<,
:lstrip!, :dasherize, :prepend, :replace, :strip, :split, …Run Code Online (Sandbox Code Playgroud) 这是Author模型和Book模型
class Author < ApplicationRecord
has_many :books, dependent: :destroy
end
class Book < ApplicationRecord
belongs_to :author
end
Run Code Online (Sandbox Code Playgroud)
我创建了2位作者(author1和author2),并为每位作者添加了2本书。
之后,我删除了author1并创建了一个新作者(author3)。现在,我想给author3两本author1的书。
是否有ActiveRecord方法用新作者来更新书籍author_id?
ruby ×7
arrays ×2
javascript ×2
activerecord ×1
associations ×1
clojure ×1
forms ×1
hash ×1
html ×1
inheritance ×1
irb ×1
jquery ×1
jruby ×1
json ×1
mixins ×1
mysql ×1
oop ×1
parameters ×1
select ×1
sql ×1
where ×1