如何比较两个数组的内容?

时间:2023-01-21 12:05:52

I am comparing zip codes.

我正在比较邮政编码。

I have three constants of zip codes :

我有三个常用的邮政编码:

ZIP_MORRIS
ZIP_UNION
ZIP_ESSEX

I want to see if a user has in an Object's array, all the zips included in one of those.

我想看看用户是否在Object的数组中,其中一个包含在其中。

I tried this:

我试过这个:

ZIP_UNION.sort{|x,y| y <=> x} <=> Email.find(3).distributions.map(&:zip_code).uniq.compact.sort{|x,y| y <=> x}

But unfortunately, this just maps all the zip codes, so if I were to choose one extra zip in a different county, then it would not properly compare them.

但不幸的是,这只是映射了所有邮政编码,所以如果我要在不同的县选择一个额外的邮政编码,那么它就不能正确地比较它们。

I think the best solution would be to compare the values of the User Generated Zips, and see if all of the zips in one ZIP_COUNTY are present inside the array.

我认为最好的解决方案是比较用户生成的拉链的值,并查看一个ZIP_COUNTY中的所有拉链是否都存在于数组中。

Some kind of iterator that would run through all the zips and ensure that the user's zip do or do not include every single zip in a zip group.

某种迭代器会遍历所有拉链并确保用户的zip包含或不包含zip组中的每个zip。

Any ideas?

有任何想法吗?

3 个解决方案

#1


69  

You can do array differences, if the result is the empty array, the 2 arrays contained the same elements:

您可以执行数组差异,如果结果是空数组,则2个数组包含相同的元素:

>> [1,2,3]-[3,1,2] #=> []

If you still have elements left, then not all elements of the first array were present in the second one:

如果仍然有元素,那么第一个数组的所有元素都不会出现在第二个元素中:

>> [1,2,5]-[3,1,2] #=> [5]

#2


8  

Below I'm using the all? operator on an array, which will return true if all of the items in the array return true for the block I'm passing in.

下面我正在使用全部?数组上的运算符,如果数组中的所有项对于我传入的块返回true,则返回true。

my_zip = [1,2,3,4,5,6]
[2,3,5].all?{|z| my_zip.include?(z)}
=> true 
[20,3,5].all?{|z| my_zip.include?(z)}
=> false

You'd just change it up to be the user's zip codes

您只需将其更改为用户的邮政编码即可

#3


5  

> [1,2,3] <=> [1,2,3]
=> 0
> [1,2,3] <=> [2,2,3]
=> -1
> [1,2,3] <=> [3,2,3]
=> -1
> [1,2,3] <=> [1,3,3]
=> -1
> [1,2,3] <=> [1,1,3]
=> 1

this is from RailsThinker Post and has been working for me very well.

这是来自RailsThinker Post并且一直为我工作。

#1


69  

You can do array differences, if the result is the empty array, the 2 arrays contained the same elements:

您可以执行数组差异,如果结果是空数组,则2个数组包含相同的元素:

>> [1,2,3]-[3,1,2] #=> []

If you still have elements left, then not all elements of the first array were present in the second one:

如果仍然有元素,那么第一个数组的所有元素都不会出现在第二个元素中:

>> [1,2,5]-[3,1,2] #=> [5]

#2


8  

Below I'm using the all? operator on an array, which will return true if all of the items in the array return true for the block I'm passing in.

下面我正在使用全部?数组上的运算符,如果数组中的所有项对于我传入的块返回true,则返回true。

my_zip = [1,2,3,4,5,6]
[2,3,5].all?{|z| my_zip.include?(z)}
=> true 
[20,3,5].all?{|z| my_zip.include?(z)}
=> false

You'd just change it up to be the user's zip codes

您只需将其更改为用户的邮政编码即可

#3


5  

> [1,2,3] <=> [1,2,3]
=> 0
> [1,2,3] <=> [2,2,3]
=> -1
> [1,2,3] <=> [3,2,3]
=> -1
> [1,2,3] <=> [1,3,3]
=> -1
> [1,2,3] <=> [1,1,3]
=> 1

this is from RailsThinker Post and has been working for me very well.

这是来自RailsThinker Post并且一直为我工作。