Rails:循环比较两个数组并将匹配推送到新数组
Rails: loop comparing two arrays and pushing matches to new array
我有两个数组需要相互检查。找到匹配项后,我需要取消第一个数组中的值并将该值推送到新数组。
这是一个例子:
第一个数组import_data
["mario", "blue", "SGC", "CJ672PA0"],
["solid snake", "green", "NID", "VI6965KD"],
["samus", "maroon", "TRUST", "DNYU6539"],
["deckard cain", "purple", "JAFA", "SJW252MZ"],
["wedge", "yellow", "WALTER", "UJ28NVM1"]
第二个数组current_data
["gordon", "orange", "DRONE", "OGJR8C0D"],
["samus", "black", "TRUST", "DNYU6539"],
["commander shepard", "red", "WHX", "TH985OI3"]
请注意,"samus" 出现在两个数组中,数据完全相同,但颜色有所变化。
我的意图 是遍历 import_data
并检查数组中每个项目的最后一个索引(8 位数字 "id")并进行比较反对 current_data
。每次找到 "match"(基于 8 位 "id")我想将该项目推入一个新数组 update_data
然后我想 unset
匹配在 import_data
数组中。
问题是两个数组的长度不同。因此,一旦我的循环超出一项的长度,它就无法再检查导致错误的索引。
我的代码基本上是这样的:
import_data.each_with_index do |data, index|
if data[3] == current_data[index].id
# @update_data << somehow push all of the data
end
end
这不起作用的原因是 import_data
的长度比 current_data
的长度长。这导致错误:
"undefined method `id' for nil:NilClass"
理想情况下最终结果如下:
"samus" 已从 import_data
数组中删除并推入 update_data
数组
import_data
循环后的数组
["mario", "blue", "SGC", "CJ672PA0"],
["solid snake", "green", "NID", "VI6965KD"],
---
["deckard cain", "purple", "JAFA", "SJW252MZ"],
["wedge", "yellow", "WALTER", "UJ28NVM1"]
新填充的数组update_data
["samus", "maroon", "TRUST", "DNYU6539"]
完成您正在做的事情的一种方法:
import_data = [
["mario", "blue", "SGC", "CJ672PA0"],
["solid snake", "green", "NID", "VI6965KD"],
["samus", "maroon", "TRUST", "DNYU6539"],
["deckard cain", "purple", "JAFA", "SJW252MZ"],
["wedge", "yellow", "WALTER", "UJ28NVM1"]
]
current_data = [
["gordon", "orange", "DRONE", "OGJR8C0D"],
["samus", "black", "TRUST", "DNYU6539"],
["commander shepard", "red", "WHX", "TH985OI3"]
]
找到两个数组的键:
import_data_keys = import_data.map { |x| x[3] }
=> ["CJ672PA0", "VI6965KD", "DNYU6539", "SJW252MZ", "UJ28NVM1"]
current_data_keys = current_data.map { |x| x[3] }
=> ["OGJR8C0D", "DNYU6539", "TH985OI3"]
通过将这两个数组相交来找到公共键:
common_keys = import_data_keys & current_data_keys
=> ["DNYU6539"]
使用 common_keys
从 import_data
删除并推送到 update_data
。
common_keys.each do |key|
import_data.each_with_index do |data, index|
if data[3] == key
update_data << import_data[index]
import_data.delete_at(index)
break
end
end
end
注意:break
语句用于在找到并处理元素后停止迭代。
由于 Prakash 的答案有效,我认为这更直接:
import_data = [
["mario", "blue", "SGC", "CJ672PA0"],
["solid snake", "green", "NID", "VI6965KD"],
["samus", "maroon", "TRUST", "DNYU6539"],
["deckard cain", "purple", "JAFA", "SJW252MZ"],
["wedge", "yellow", "WALTER", "UJ28NVM1"]
]
current_data = [
["gordon", "orange", "DRONE", "OGJR8C0D"],
["samus", "black", "TRUST", "DNYU6539"],
["commander shepard", "red", "WHX", "TH985OI3"]
]
current_ids = current_data.map {|c| c[3]}
update_data = import_data.inject([]) do |result, data|
result << import_data.delete(data) if current_ids.include?(data[3])
result
end
在运行之后你得到:
update_data = [
["samus", "maroon", "TRUST", "DNYU6539"]
]
import_data = [
["mario", "blue", "SGC", "CJ672PA0"],
["solid snake", "green", "NID", "VI6965KD"],
["deckard cain", "purple", "JAFA", "SJW252MZ"],
["wedge", "yellow", "WALTER", "UJ28NVM1"]
]
希望对您有所帮助。
我有两个数组需要相互检查。找到匹配项后,我需要取消第一个数组中的值并将该值推送到新数组。
这是一个例子:
第一个数组import_data
["mario", "blue", "SGC", "CJ672PA0"],
["solid snake", "green", "NID", "VI6965KD"],
["samus", "maroon", "TRUST", "DNYU6539"],
["deckard cain", "purple", "JAFA", "SJW252MZ"],
["wedge", "yellow", "WALTER", "UJ28NVM1"]
第二个数组current_data
["gordon", "orange", "DRONE", "OGJR8C0D"],
["samus", "black", "TRUST", "DNYU6539"],
["commander shepard", "red", "WHX", "TH985OI3"]
请注意,"samus" 出现在两个数组中,数据完全相同,但颜色有所变化。
我的意图 是遍历 import_data
并检查数组中每个项目的最后一个索引(8 位数字 "id")并进行比较反对 current_data
。每次找到 "match"(基于 8 位 "id")我想将该项目推入一个新数组 update_data
然后我想 unset
匹配在 import_data
数组中。
问题是两个数组的长度不同。因此,一旦我的循环超出一项的长度,它就无法再检查导致错误的索引。
我的代码基本上是这样的:
import_data.each_with_index do |data, index|
if data[3] == current_data[index].id
# @update_data << somehow push all of the data
end
end
这不起作用的原因是 import_data
的长度比 current_data
的长度长。这导致错误:
"undefined method `id' for nil:NilClass"
理想情况下最终结果如下:
"samus" 已从 import_data
数组中删除并推入 update_data
数组
import_data
循环后的数组
["mario", "blue", "SGC", "CJ672PA0"],
["solid snake", "green", "NID", "VI6965KD"],
---
["deckard cain", "purple", "JAFA", "SJW252MZ"],
["wedge", "yellow", "WALTER", "UJ28NVM1"]
新填充的数组update_data
["samus", "maroon", "TRUST", "DNYU6539"]
完成您正在做的事情的一种方法:
import_data = [
["mario", "blue", "SGC", "CJ672PA0"],
["solid snake", "green", "NID", "VI6965KD"],
["samus", "maroon", "TRUST", "DNYU6539"],
["deckard cain", "purple", "JAFA", "SJW252MZ"],
["wedge", "yellow", "WALTER", "UJ28NVM1"]
]
current_data = [
["gordon", "orange", "DRONE", "OGJR8C0D"],
["samus", "black", "TRUST", "DNYU6539"],
["commander shepard", "red", "WHX", "TH985OI3"]
]
找到两个数组的键:
import_data_keys = import_data.map { |x| x[3] }
=> ["CJ672PA0", "VI6965KD", "DNYU6539", "SJW252MZ", "UJ28NVM1"]
current_data_keys = current_data.map { |x| x[3] }
=> ["OGJR8C0D", "DNYU6539", "TH985OI3"]
通过将这两个数组相交来找到公共键:
common_keys = import_data_keys & current_data_keys
=> ["DNYU6539"]
使用 common_keys
从 import_data
删除并推送到 update_data
。
common_keys.each do |key|
import_data.each_with_index do |data, index|
if data[3] == key
update_data << import_data[index]
import_data.delete_at(index)
break
end
end
end
注意:break
语句用于在找到并处理元素后停止迭代。
由于 Prakash 的答案有效,我认为这更直接:
import_data = [
["mario", "blue", "SGC", "CJ672PA0"],
["solid snake", "green", "NID", "VI6965KD"],
["samus", "maroon", "TRUST", "DNYU6539"],
["deckard cain", "purple", "JAFA", "SJW252MZ"],
["wedge", "yellow", "WALTER", "UJ28NVM1"]
]
current_data = [
["gordon", "orange", "DRONE", "OGJR8C0D"],
["samus", "black", "TRUST", "DNYU6539"],
["commander shepard", "red", "WHX", "TH985OI3"]
]
current_ids = current_data.map {|c| c[3]}
update_data = import_data.inject([]) do |result, data|
result << import_data.delete(data) if current_ids.include?(data[3])
result
end
在运行之后你得到:
update_data = [
["samus", "maroon", "TRUST", "DNYU6539"]
]
import_data = [
["mario", "blue", "SGC", "CJ672PA0"],
["solid snake", "green", "NID", "VI6965KD"],
["deckard cain", "purple", "JAFA", "SJW252MZ"],
["wedge", "yellow", "WALTER", "UJ28NVM1"]
]
希望对您有所帮助。