数组中唯一值_唯一数组值

数组中唯一值

When you look at any programming language, you see missing features that you find puzzling because the use case seems so common.  One such case is retrieving unique values from an array with JavaScript. Years ago I mentioned an easy way of unique value management using objects instead of arrays, but that's not always an option and doesn't match every use case.

当您使用任何一种编程语言时,都会发现缺少的功能,这些功能令人困惑,因为用例似乎很常见。 一种这样的情况是使用JavaScript从数组中检索唯一值。 几年前,我提到了一种使用对象而不是数组进行唯一值管理简便方法 ,但这并不总是一种选择,并且并不适合每个用例。

Want to retrieve a unique array of values from an array that may include duplicate values?  You can use new JavaScript spread operator with Set to get an array of unique values:

是否想从可能包含重复值的数组中检索值的唯一数组? 您可以将新JavaScript Spread运算符与Set一起使用,以获取唯一值数组:


var j = [...new Set([1, 2, 3, 3])]
>> [1, 2, 3]


Getting unique array values is another awesome usage of the spread operator.  And don't forget you can merge object properties with the spread operator!

获得唯一的数组值是散布运算符的另一种很棒的用法 。 并且不要忘记,您可以将对象属性与传播运算符合并!

There's no better feeling than being able to remove a library to complete a task that should be native to the language.  This trick brings us one step closer to that!

没有什么比删除库来完成该语言固有的任务更好的感觉了。 这个技巧使我们更接近了这一步!

翻译自: https://davidwalsh.name/array-unique

数组中唯一值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值