Javascript 如何将这个ruby字符串转换为普通数组?

Javascript 如何将这个ruby字符串转换为普通数组?,javascript,ruby,arrays,Javascript,Ruby,Arrays,我正在检索一个如下所示的字符串: "[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], [], []] [[[256, 498]], [], [], []]" 我怎样才能让它看起来像这样 [[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[ 正在寻找ruby或Jav

我正在检索一个如下所示的字符串:

"[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], [], []] [[[256, 498]], [], [], []]"
我怎样才能让它看起来像这样

[[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[256498]、[


正在寻找ruby或JavaScript的解决方案。

仍在寻找更优雅的解决方案,但这会奏效

str.scan(/\[+(\d*, ?\d*)?\]+/).flatten.map do |a|
  a ? a.split(",").map(&:to_i) : []
end
#=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []]
方法的细目

scanned_string_array = str.scan(/\[+(\d*, ?\d*)?\]+/)
#=> [["256, 498"], ["256, 498"], [nil], ["256, 498"], [nil], [nil], ["256, 498"], [nil], [nil], [nil]]
scanned_string_array.flatten
#=> ["256, 498", "256, 498", nil, "256, 498", nil, nil, "256, 498", nil, nil, nil]
scanned_string_array.flatten.map do |a|
  #if a is nil return empty array otherwise split on comma and map to Integer
  a ? a.split(",").map(&:to_i) : []
end
#=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []]
更新:

找到一种更优雅的方式,或者至少我更喜欢它

str.scan(/\[+(\d*),?\s?(\d*)?\]+/).map do |a|
  a.reject(&:empty?).map(&:to_i)
end
#=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []]

仍在寻找更优雅的解决方案,但这将起作用

str.scan(/\[+(\d*, ?\d*)?\]+/).flatten.map do |a|
  a ? a.split(",").map(&:to_i) : []
end
#=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []]
方法的细目

scanned_string_array = str.scan(/\[+(\d*, ?\d*)?\]+/)
#=> [["256, 498"], ["256, 498"], [nil], ["256, 498"], [nil], [nil], ["256, 498"], [nil], [nil], [nil]]
scanned_string_array.flatten
#=> ["256, 498", "256, 498", nil, "256, 498", nil, nil, "256, 498", nil, nil, nil]
scanned_string_array.flatten.map do |a|
  #if a is nil return empty array otherwise split on comma and map to Integer
  a ? a.split(",").map(&:to_i) : []
end
#=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []]
更新:

找到一种更优雅的方式,或者至少我更喜欢它

str.scan(/\[+(\d*),?\s?(\d*)?\]+/).map do |a|
  a.reject(&:empty?).map(&:to_i)
end
#=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []]

我将按如下方式提取数组

代码

def extract(str)
  str.scan(/\[[^\[\]]*\]/).map { |s| s.scan(/\d+/).map(&:to_i) }
end
str = "[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], " \
      "[], []] [[[256, 498]], [], [], []]"
extract(str)
  #=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []] 
require 'benchmark'

@indent = @methods.map { |m| m.to_s.size }.max

def test(str, reps)
  exit if answers_not_all_the_same(str)
  Benchmark.bm(@indent) do |bm|
    @methods.each do |m|
      bm.report m.to_s do
        reps.times { compute(m, str) }
      end
    end
  end
end

def answers_not_all_the_same(str)
  same = @methods.map { |m| compute(m, str) }.uniq.size > 1
  puts same ? "all don't match" : "all match"
end
示例

def extract(str)
  str.scan(/\[[^\[\]]*\]/).map { |s| s.scan(/\d+/).map(&:to_i) }
end
str = "[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], " \
      "[], []] [[[256, 498]], [], [], []]"
extract(str)
  #=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []] 
require 'benchmark'

@indent = @methods.map { |m| m.to_s.size }.max

def test(str, reps)
  exit if answers_not_all_the_same(str)
  Benchmark.bm(@indent) do |bm|
    @methods.each do |m|
      bm.report m.to_s do
        reps.times { compute(m, str) }
      end
    end
  end
end

def answers_not_all_the_same(str)
  same = @methods.map { |m| compute(m, str) }.uniq.size > 1
  puts same ? "all don't match" : "all match"
end
解释

对于上述示例:

str.scan
提取格式为
“[…]”
的所有字符串,其中
是除大括号和大括号以外的字符:

a = str.scan(/\[[^\[\]]*\]/)
  #=> ["[256, 498]", "[256, 498]", "[]", "[256, 498]",
  #    "[]", "[]", "[256, 498]", "[]", "[]", "[]"]
map
然后将
a
的每个元素传递给其块,并将该值分配给块变量
s
,第一个是:

s = "[256, 498]"
然后执行以下操作:

ss = s.scan(/\d+/) #=> ["256", "498"] 
ss.map(&:to_i)     #=> [256, 498] 
“[]”
被传递到块中时:

s = "[]"
ss = s.scan(/\d+/) #=> []
ss.map(&:to_i)     #=> []
基准

我曾要求对各种方法进行基准比较。我很高兴满足这一要求,并在下文报告结果。唉,我的解决方案并没有那么好,但当某个工程师的姓氏不包含元音(除了“有时是y”)时,对基准测试的请求似乎总是发生的

我只做了一个基准测试,但是当我改变测试数据时,结果是相似的

比较的方法

module Methods
  def smnky1(str)
    str.scan(/\[+(\d*, ?\d*)?\]+/).flatten.map do |a|
      a ? a.split(",").map(&:to_i) : []
    end
  end

  def smnky2(str)
    str.scan(/\[+(\d*),?\s?(\d*)?\]+/).map do |a|
      a.reject(&:empty?).map(&:to_i)
    end
  end

  def cary(str)
    str.scan(/\[[^\[\]]*\]/).map { |s| s.scan(/\d+/).map(&:to_i) }
  end
end

@methods = Methods.instance_methods(false)
include Methods
def compute(m, str) send(m, str) end
基准代码

def extract(str)
  str.scan(/\[[^\[\]]*\]/).map { |s| s.scan(/\d+/).map(&:to_i) }
end
str = "[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], " \
      "[], []] [[[256, 498]], [], [], []]"
extract(str)
  #=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []] 
require 'benchmark'

@indent = @methods.map { |m| m.to_s.size }.max

def test(str, reps)
  exit if answers_not_all_the_same(str)
  Benchmark.bm(@indent) do |bm|
    @methods.each do |m|
      bm.report m.to_s do
        reps.times { compute(m, str) }
      end
    end
  end
end

def answers_not_all_the_same(str)
  same = @methods.map { |m| compute(m, str) }.uniq.size > 1
  puts same ? "all don't match" : "all match"
end
结果

str = "[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], " \
      "[], []] [[[256, 498]], [], [], []]"
reps = 100_000
puts "Example string, #{reps} repetitions"
test(str, reps)

Example string, 100000 repetitions
all match
             user     system      total        real
smnky1   1.830000   0.000000   1.830000 (  1.830457)
smnky2   1.920000   0.010000   1.930000 (  1.920094)
cary     2.750000   0.000000   2.750000 (  2.752946)

我将按如下方式提取数组

代码

def extract(str)
  str.scan(/\[[^\[\]]*\]/).map { |s| s.scan(/\d+/).map(&:to_i) }
end
str = "[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], " \
      "[], []] [[[256, 498]], [], [], []]"
extract(str)
  #=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []] 
require 'benchmark'

@indent = @methods.map { |m| m.to_s.size }.max

def test(str, reps)
  exit if answers_not_all_the_same(str)
  Benchmark.bm(@indent) do |bm|
    @methods.each do |m|
      bm.report m.to_s do
        reps.times { compute(m, str) }
      end
    end
  end
end

def answers_not_all_the_same(str)
  same = @methods.map { |m| compute(m, str) }.uniq.size > 1
  puts same ? "all don't match" : "all match"
end
示例

def extract(str)
  str.scan(/\[[^\[\]]*\]/).map { |s| s.scan(/\d+/).map(&:to_i) }
end
str = "[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], " \
      "[], []] [[[256, 498]], [], [], []]"
extract(str)
  #=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []] 
require 'benchmark'

@indent = @methods.map { |m| m.to_s.size }.max

def test(str, reps)
  exit if answers_not_all_the_same(str)
  Benchmark.bm(@indent) do |bm|
    @methods.each do |m|
      bm.report m.to_s do
        reps.times { compute(m, str) }
      end
    end
  end
end

def answers_not_all_the_same(str)
  same = @methods.map { |m| compute(m, str) }.uniq.size > 1
  puts same ? "all don't match" : "all match"
end
解释

对于上述示例:

str.scan
提取格式为
“[…]”
的所有字符串,其中
是除大括号和大括号以外的字符:

a = str.scan(/\[[^\[\]]*\]/)
  #=> ["[256, 498]", "[256, 498]", "[]", "[256, 498]",
  #    "[]", "[]", "[256, 498]", "[]", "[]", "[]"]
map
然后将
a
的每个元素传递给其块,并将该值分配给块变量
s
,第一个是:

s = "[256, 498]"
然后执行以下操作:

ss = s.scan(/\d+/) #=> ["256", "498"] 
ss.map(&:to_i)     #=> [256, 498] 
“[]”
被传递到块中时:

s = "[]"
ss = s.scan(/\d+/) #=> []
ss.map(&:to_i)     #=> []
基准

我曾要求对各种方法进行基准比较。我很高兴满足这一要求,并在下文报告结果。唉,我的解决方案并没有那么好,但当某个工程师的姓氏不包含元音(除了“有时是y”)时,对基准测试的请求似乎总是发生的

我只做了一个基准测试,但是当我改变测试数据时,结果是相似的

比较的方法

module Methods
  def smnky1(str)
    str.scan(/\[+(\d*, ?\d*)?\]+/).flatten.map do |a|
      a ? a.split(",").map(&:to_i) : []
    end
  end

  def smnky2(str)
    str.scan(/\[+(\d*),?\s?(\d*)?\]+/).map do |a|
      a.reject(&:empty?).map(&:to_i)
    end
  end

  def cary(str)
    str.scan(/\[[^\[\]]*\]/).map { |s| s.scan(/\d+/).map(&:to_i) }
  end
end

@methods = Methods.instance_methods(false)
include Methods
def compute(m, str) send(m, str) end
基准代码

def extract(str)
  str.scan(/\[[^\[\]]*\]/).map { |s| s.scan(/\d+/).map(&:to_i) }
end
str = "[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], " \
      "[], []] [[[256, 498]], [], [], []]"
extract(str)
  #=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []] 
require 'benchmark'

@indent = @methods.map { |m| m.to_s.size }.max

def test(str, reps)
  exit if answers_not_all_the_same(str)
  Benchmark.bm(@indent) do |bm|
    @methods.each do |m|
      bm.report m.to_s do
        reps.times { compute(m, str) }
      end
    end
  end
end

def answers_not_all_the_same(str)
  same = @methods.map { |m| compute(m, str) }.uniq.size > 1
  puts same ? "all don't match" : "all match"
end
结果

str = "[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], " \
      "[], []] [[[256, 498]], [], [], []]"
reps = 100_000
puts "Example string, #{reps} repetitions"
test(str, reps)

Example string, 100000 repetitions
all match
             user     system      total        real
smnky1   1.830000   0.000000   1.830000 (  1.830457)
smnky2   1.920000   0.010000   1.930000 (  1.920094)
cary     2.750000   0.000000   2.750000 (  2.752946)


这应该是有效的数组语法吗?与论坛网站不同,我们不使用“谢谢”或“感谢任何帮助”或签名。请参阅“.顺便说一句,这是“提前感谢”,而不是“提前感谢”。这应该是有效的数组语法吗?与论坛网站不同,我们不使用“感谢”、“感谢任何帮助”或签名。请参阅”。顺便说一句,它是“提前感谢”,而不是“提前感谢”。这看起来非常优雅,因为它返回了一个有效的数组定义。@theTinMan谢谢。我在寻找更好地处理
的方法。我用一个更优雅的解决方案更新了我的帖子。这真的很棒,非常感谢@Jay可能值得注意的是,虽然我更喜欢第二种方法,但第一种方法的速度要快约10%+/-1%。尽管这取决于此方法的频率和字符串的大小,但差异可能无关紧要。这看起来非常优雅,因为它返回了一个有效的数组定义。@theTinMan谢谢。我在寻找更好地处理
的方法。我用一个更优雅的解决方案更新了我的帖子。这真的很棒,非常感谢@Jay可能值得注意的是,虽然我更喜欢第二种方法,但第一种方法的速度要快约10%+/-1%。虽然这取决于这种方法的频率和字符串的大小,但差异可能无关紧要。介意把它和我的相比吗?我不在电脑旁。如果不行,我明天就做。我一直对你能找到另一种方法来处理问题的能力印象深刻。@engineersmnky,你的愿望就是我的命令。谢谢。ruby和它的追随者总是以干净清晰的代码而自豪,而这些方法中没有一种是“闻”出来的,在我看来,它们中“最脏”的执行速度最快。如果可以的话,我会再次投票,以确保你的答案的完整性。介意把这个和我的答案对立起来吗?我不在电脑旁。如果不行,我明天就做。我一直对你能找到另一种方法来处理问题的能力印象深刻。@engineersmnky,你的愿望就是我的命令。谢谢。ruby和它的追随者总是以干净清晰的代码而自豪,而这些方法中没有一种是“闻”出来的,在我看来,它们中“最脏”的执行速度最快。如果可以的话,我会再次投票支持你的答案的完整性。