我有一个数组如下:
['a=1', 'b=2', 'a=2']
我想对数组进行过滤,以使元素相对于其第一个字符而言是唯一的。我想要结果:
['a=1', 'b=2']
关于以上内容,["a", "b", "a"]
变为["a", "b"]
。
感谢所有帮助。
这是来自Array#uniq的注释文档:
b = [["student","sam"], ["student","george"], ["teacher","matz"]]
b.uniq { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]
因此,似乎b.uniq{ |s| s[0] }
可以通过每个字符串的第一个字符来使数组唯一。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句