_.uniq_在Ruby中使用Array.compact和Array.uniq方法从Array中移除元素
_.uniq
Ruby Array.compact和Array.uniq方法 (Ruby Array.compact and Array.uniq Methods)
In the last article, we have gone through two different methods of deleting elements from the Array. We have seen their implementation with the help of their syntaxes and supporting examples. For a quick recall, let me tell those two methods were Array.delete() and Array.delete_at(). Array.delete() is a method that requires the name of the element which is supposed to be passed in the argument list of the Array. It works in a way that it deletes all the elements of the same name as mentioned in the argument list of Array.delete() whereas Array.delete_at() requires the index of the element we want to remove from the Array.
在上一篇文章中,我们介绍了从Array删除元素的两种不同方法 。 我们已经在它们的语法和支持示例的帮助下看到了它们的实现。 为了快速回顾一下,让我说说这两个方法是Array.delete()和Array.delete_at() 。 Array.delete()是一种方法,它需要假定应该在Array的参数列表中传递的元素的名称。 它的工作方式是删除与Array.delete()的参数列表中提到的名称相同的所有元素,而Array.delete_at()则需要我们要从Array中删除的元素的索引。
In the article, we will learn about two more and important methods of deleting the elements from the instance of Array class namely Array.comp() and Array.uniq()!.
在本文中,我们将学习另外两个重要的删除Array类实例中元素的方法,即Array.comp()和Array.uniq()! 。
Now let us go through their implementation with the help of their syntaxes and examples which you will find in the rest of the article.
现在,让我们在它们的语法和示例的帮助下完成它们的实现,您将在本文的其余部分中找到它们。
1)Array.compact方法 (1) Array.compact Method)
Parameter(s):
参数:
This method requires no arguments.
此方法不需要任何参数。
Syntax:
句法:
Array.compact # or Array.compact![/code]
Method description:
方法说明:
In most of the cases, you will find that your array is containing some nil values and you will be requiring some mechanism or lines of code to delete those nil values which are present in your Array. Ruby decreases your overhead of writing code for the same purpose by providing you a method named Array.compact. This method is used to remove all the nil values from the object of the Array class. Array.compact! is used to make changes in the Array as well.
在大多数情况下,您会发现数组中包含一些nil值,并且需要某种机制或代码行来删除数组中存在的这些nil值。 Ruby通过为您提供一个名为Array.compact的方法来减少出于相同目的而编写代码的开销。 此方法用于从Array类的对象中删除所有nil值。 Array.compact! 也用于在Array中进行更改。
Example:
例:
=begin Ruby program to remove elements from Array using Array.compact =end # array declaration Adc = [nil,'Includehelp.com','Ruby','C++','C#','Java','Python','C++',nil,nil] # Array.compact method puts "Array.compact!" Adc.compact # printing array elements puts "Array elements are:" print Adc puts "" # Array.compact! method puts "Array.compact!" Adc.compact! # printing array elements puts "Array elements are:" print Adc[/code]
Output
输出量
Array.compact! Array elements are: [nil, "Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++", nil, nil] Array.compact! Array elements are: ["Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++"][/code]
Explanation:
说明:
In the above code, you can observe that Array.compact can be used to remove nil elements from the Array whereas Array.compact! can be used to make changes in the same Array as well.
在上面的代码中,您可以观察到Array.compact可用于从Array中删除nil个元素,而Array.compact! 也可以用于在同一Array中进行更改。
2)Array.uniq方法 (2) Array.uniq Method)
Parameter (s):
参数:
No arguments required.
无需参数。
Syntax:
句法:
Array.uniq # or Array.uniq![/code]
Method description:
方法说明:
Array.uniq or Array.uniq! can be used to remove duplicate elements from the Array. Sometimes you may observe that your Array is containing duplicate entries which may create any kind of complexity. Array.uniq is non-destructive whereas Array.uniq! is destructive, which means that the latter will create changes in the same Array as well.
Array.uniq或Array.uniq! 可用于从数组中删除重复的元素。 有时,您可能会发现您的数组包含重复的条目,这可能会造成任何形式的复杂性。 Array.uniq是非破坏性的,而Array.uniq! 是破坏性的,这意味着后者也会在同一Array中创建更改。
Example:
例:
=begin Ruby program to remove elements from Array using Array.uniq =end # array declaration Adc = ['Ruby','Includehelp.com','Ruby','C++','C#','Java','Python','C++','Java'] # Array.uniq Method puts "Array.uniq" print Adc.uniq puts "" puts "Array elements are:" print Adc puts "" # Array.uniq! Method puts "Array.uniq!" Adc.uniq! puts "Array elements are:" print Adc[/code]
Output
输出量
Array.uniq ["Ruby", "Includehelp.com", "C++", "C#", "Java", "Python"] Array elements are: ["Ruby", "Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++", "Java"] Array.uniq! Array elements are: ["Ruby", "Includehelp.com", "C++", "C#", "Java", "Python"][/code]
Explanation:
说明:
In the above code, you can see that Array.uniq is not creating any change in the Array whereas it is possible with the help of Array.uniq! method. You can go for Sets if you want to avoid duplicacy.
在上面的代码中,您可以看到Array.uniq并没有在Array中创建任何更改,而借助Array.uniq可以实现! 方法。 如果要避免重复,可以选择Sets。
_.uniq
- ruby array_在Ruby中使用Array.first和Array.last方法访问数组的第一个和最后一个元素...
- php通过array_shift()函数移除数组第一个元素的方法
- 41. jQuery-使用unbind()方法移除元素绑定事件
- 使用data()方法在元素上存取移除JSON格式数据 10-6
- ArrayList在for循环中使用remove方法移除元素方法介绍
- 使用sortedArrayUsingSelector:方法排序数组中的元素
- 14.使用unbind()方法移除元素绑定的事件
- 使用data()方法在元素上存取移除JSON格式数据 10-6
- JavaScript使用shift方法移除素组第一个元素实例分析
- JavaScript使用pop方法移除数组最后一个元素用法实例
- 使用Set移除List中相同元素方法
- ArrayList在for循环中使用remove方法移除元素
- 【jQuery】使用unbind()方法移除元素绑定的事件
- 使用data()方法在元素上存取移除数据 10-4
- 使用data()方法在元素上存取移除数据 10-4
- 如何把枚举元素的描述输出(不使用反映和扩展方法(3.5才支持呢)
- Ruby Benchmark 模块提供了测量和报告Ruby代码运行使用时间的方法。
- $.map的使用——对数组array中每个元素遍历处理
- JSONObject与JSONArray的使用方法 .
- ruby 中使用 Dom技术来查找页面元素