Ruby on rails 以两种方式相互比较哈希数组
添加这个是因为有些人看不到我的要求,但我在每个数组中查找与其他数组相比的唯一值 我需要找出Ruby on rails 以两种方式相互比较哈希数组,ruby-on-rails,ruby,Ruby On Rails,Ruby,添加这个是因为有些人看不到我的要求,但我在每个数组中查找与其他数组相比的唯一值 我需要找出保存的\u开始和传入的\u开始之间的差异,以及传入的\u开始和保存的\u开始之间的差异saved_start是数据库查询的结果,而incomming_start是webhook的params的结果。以下是两个阵列: saved_start = [{id: "12345", qty: 25}, {id: "678", qty: 20}] incoming_start = [{id: "12347", qty:
保存的\u开始
和传入的\u开始
之间的差异,以及传入的\u开始
和保存的\u开始
之间的差异saved_start
是数据库查询的结果,而incomming_start
是webhook的params
的结果。以下是两个阵列:
saved_start = [{id: "12345", qty: 25}, {id: "678", qty: 20}]
incoming_start = [{id: "12347", qty: 25}, {id: "678", qty: 20}, {id: "abc", qty: 20}]
我发现了两个阵列之间的差异:
in_array = incoming_start - saved_start
saved_array = saved_start - incoming_start
上述申报表:
in_array => [{:id=>"12347", :qty=>25}, {:id=>"abc", :qty=>20}]
saved_array => [{:id=>"12345", :qty=>25}]
然后遍历它们,发现数组中的具有12347的id
和abc
,而保存的数组中的没有。而保存的\u数组
具有12345的id
,而在\u数组中
没有。数量也是如此
如果数组中的和保存的数组中的大小相同,则此操作效果良好。如果没有,它就坏了
in_array.each do |incoming|
saved_array.each do |saved|
pp "Does the saved hash have the id #{incoming[:id]}
#{saved.has_value?(incoming[:id])}"
pp "Does the saved hash have the quantity of
#{incoming[:qty]} #{saved.has_value?(incoming[:qty])}"
end
end
上述方法效果良好,因为in_array
大于saved_array
下面返回的数据不正确,因为保存的\u数组
较小
saved_array.each do |saved|
in_array.each do |incoming|
pp "Does the incoming hash have the id #{saved[:id]}
#{incoming.has_value?(saved[:id])}"
pp "Does the incoming hash have the quantity of #{saved[:qty]}
#{incoming.has_value?(saved[:qty])}"
end
end
在英语中,我需要使保存的\u开始
包含与传入的\u开始
相同的数据
但要做到这一点,我需要知道saved\u start
中有哪些内容不在incoming\u start
中,这样我就可以从saved\u start
中将其删除。然后找出“传入的\u开始”中哪些内容不在“保存的\u开始”中,以便我可以添加它。最后,哪一个id
在两者中,但可能quantity
在incoming\u start
中不同,因此我需要更新saved\u start
呼
谢谢您好,我希望下面的代码可能会有所帮助。此代码将:(01)-数组1中的点值在数组2中找不到。。。(02)-在数组1中找不到数组2中的点值。。。(03)-将所有唯一值存储在数组2中。。。。对于这个问题的描述,我想这就是您想要的,为了让事情更简单,我将数组重命名为:arr1&arr2
#Compare an array of hashes against each other, both ways
saved_start = [{id: "12345", qty: 25}, {id: "678", qty: 20}]
incoming_start = [{id: "12347", qty: 25}, {id: "678", qty: 20}, {id: "abc", qty: 20}]
# storing in an Array with easier name
p arr1 = saved_start
p arr2 = incoming_start
puts
# Initialize array where the Non duplicated vales will be stored
not_in_arr_1, not_in_arr_2 = [], []
# Check if the arrays are same
if (arr1 == arr2) == true then
puts "Arrays are equal"
else
puts "Arrays NOT Equals"
# ----------------------------
# Storing Values from array 1 not found in array 2
arr1.length.times do |x|
if arr2.include?(arr1[x]) == false then
not_in_arr_2 << arr1[x]
end
end
# ----------------------------
# Storing Values from array 2 not found in array 1
arr2.length.times do |x|
if arr1.include?(arr2[x]) == false then
not_in_arr_1 << arr2[x]
end
end
# ----------------------------
# concatenating original Array2 + values of Array1 not found in Array 2
arr2.concat not_in_arr_2
# ----------------------------
# Console Printing Results
puts
puts "Values in array 1, NOT found in array 2:"
p not_in_arr_2
puts
puts "Values in array 2, NOT found in array 1:"
p not_in_arr_1
puts
puts "Array 2 with updated values:"
p arr2
# ----------------------------
end
# ----------------------------------------
# Test Run
#
# [{:id=>"12345", :qty=>25}, {:id=>"678", :qty=>20}]
# [{:id=>"12347", :qty=>25}, {:id=>"678", :qty=>20}, {:id=>"abc", :qty=>20}]
#
# Arrays NOT Equals
#
# Values in array 1, NOT found in array 2:
# [{:id=>"12345", :qty=>25}]
#
# Values in array 2, NOT found in array 1:
# [{:id=>"12347", :qty=>25}, {:id=>"abc", :qty=>20}]
#
# Array 2 with updated values:
# [{:id=>"12347", :qty=>25}, {:id=>"678", :qty=>20}, {:id=>"abc", :qty=>20}, {:id=>"12345", :qty=>25}
#
# ----------------------------------------
#以两种方式比较哈希数组
保存的_start=[{id:“12345”,数量:25},{id:“678”,数量:20}]
传入的_start=[{id:“12347”,数量:25},{id:“678”,数量:20},{id:“abc”,数量:20}]
#以更简单的名称存储在阵列中
p arr1=保存的\u开始
p arr2=输入\u启动
放
#初始化存储非重复值的数组
不在1,不在2=[],[]
#检查阵列是否相同
如果(arr1==arr2)==true,则
放置“数组相等”
其他的
将“数组不等于”
# ----------------------------
#存储数组2中未找到的数组1中的值
arr1.length.times do | x|
如果arr2.include?(arr1[x])==false,则
不在{u arr_2 25},{:id=>“678”,:qty=>20}]
#[{:id=>12347',:qty=>25},{:id=>678',:qty=>20},{:id=>abc',:qty=>20}]
#
#数组不等于
#
#数组1中的值,在数组2中找不到:
#[{:id=>12345,:qty=>25}]
#
#数组2中的值,在数组1中找不到:
#[{:id=>12347',:qty=>25},{:id=>abc',:qty=>20}]
#
#具有更新值的数组2:
#[{:id=>“12347”、:qty=>25}、{:id=>“678”、:qty=>20}、{:id=>“abc”、:qty=>20}、{:id=>“12345”、:qty=>25}
#
# ----------------------------------------
我希望这会有所帮助。请发布您希望收到的结果。更新!请查看问题底部的英文。为什么不saved\u start=incoming\u start
?因为这样我知道有区别,但区别在哪里?在saved
或incoming
中,还有什么值不同erent?请以[1,2,3,4,5]
和[4,5,6,7]
为例,说明所需的输出应该是什么。如果它适用于整数数组,它也适用于其他数组。