我有两个数组
arr1 = [[["name", "Alex"],["age", "4"], ["width", "55"], ["weight", "30"], ["species", "Alex"], ["female", "yes"], ["group"]], [["name", "All"],["age", "7"], ["width", "26"], ["weight", "3"], ["species", "cat"], ["female", "no"], ["group"]]]
arr2 = [["A23", "All", "Katy", "Max"], ["B23", "Sisi", "Alex"]]
如果名称,例如。 arr2的值Alex与arr1中的值Alex类似,arr2中的值B23被推送到子阵列[“group”]到第一个数组。所以我想得到
arr1 = [[["name", "Alex"],["age", "4"], ["width", "55"], ["weight", "30"], ["species", "Alex"], ["female", "yes"], ["group", "B23"]], [["name", "All"],["age", "7"], ["width", "26"], ["weight", "3"], ["species", "cat"], ["female", "no"], ["group", "A23"]]]
如何比较和组合这些数组?
答案 0 :(得分:1)
您没有提供任何代码,因此我不会写出完整的解决方案。正如@EddeAlmeida评论的那样,使用哈希数组会更容易。这是一个将数据快速转换的基本结构:
require 'pp'
arr1 = [[["name", "Alex"],["age", "4"], ["width", "55"], ["weight", "30"], ["species", "Alex"], ["female", "yes"], ["group"]], [["name", "All"],["age", "7"], ["width", "26"], ["weight", "3"], ["species", "cat"], ["female", "no"], ["group"]]]
arr2 = [["A23", "All", "Katy", "Max"], ["B23", "Sisi", "Alex"]]
data = arr1.map { |a| a.tap { |x| x.last[1] = '' }.to_h }
pp data
# [{"name"=>"Alex",
# "age"=>"4",
# "width"=>"55",
# "weight"=>"30",
# "species"=>"Alex",
# "female"=>"yes",
# "group"=>""},
# {"name"=>"All",
# "age"=>"7",
# "width"=>"26",
# "weight"=>"3",
# "species"=>"cat",
# "female"=>"no",
# "group"=>""}]
arr2.each do |code, *names|
# add some logic here
end
# Coming back to (weird) nested arrays :
p data.map{|h| h.to_a}
答案 1 :(得分:0)
我这样做了:
arr1.each do |data|
a = data[0][1]
b = ""
arr2.each do |x|
if (x.include?(a)) then
b << "#{x[0]} "
end
end
data[6].push(b)
端
答案 2 :(得分:0)
arr1 = [
[["name", "Alex"],["age", "4"], ["width", "55"], ["weight", "30"],
["species", "Alex"], ["female", "yes"], ["group"]],
[["name", "All"],["age", "7"], ["width", "26"], ["weight", "3"],
["species", "cat"], ["female", "no"], ["group"]]
]
arr2 = [["A23", "All", "Katy", "Max"], ["B23", "Sisi", "Alex"]]
这是一种允许arr1
的每个元素的元素以任何顺序排列的方式,并在包含"group"
的元素被修改后保持该顺序。
h2 = arr2.each_with_object({}) { |(first,*rest),h| rest.each { |s| h[s] = first } }
#=> {"All"=>"A23", "Katy"=>"A23", "Max"=>"A23", "Sisi"=>"B23", "Alex"=>"B23"}
arr1.map do |a|
h = a.each_with_object({}) { |(k,v),h| h[k]=v }
h["group"] = h2[h["name"]]
h.to_a
end
#=> [[["name", "Alex"], ["age", "4"], ["width", "55"], ["weight", "30"],
# ["species", "Alex"], ["female", "yes"], ["group", "B23"]],
# [["name", "All"], ["age", "7"], ["width", "26"], ["weight", "3"],
# ["species", "cat"], ["female", "no"], ["group", "A23"]]]
如果Array#map
不发生变异,请使用arr1
。
arr1
的两个元素中的第一个元素的块计算如下:
a = arr1.first
#=> [["name", "Alex"], ["age", "4"], ["width", "55"], ["weight", "30"],
# ["species", "Alex"], ["female", "yes"], ["group"]]
h = a.each_with_object({}) { |(k,v),h| h[k]=v }
#=> {"name"=>"Alex", "age"=>"4", "width"=>"55", "weight"=>"30",
# "species"=>"Alex", "female"=>"yes", "group"=>nil}
v = h["name"]
#=> "Alex"
h["group"] = h2[v]
#=> h2["Alex"] => "B23"
h #=> {"name"=>"Alex", "age"=>"4", "width"=>"55", "weight"=>"30",
# "species"=>"Alex", "female"=>"yes", "group"=>"B23"}
h.to_a
#=> [["name", "Alex"], ["age", "4"], ["width", "55"], ["weight", "30"],
# ["species", "Alex"], ["female", "yes"], ["group", "B23"]]
arr1
的第二个元素的计算类似。