javascript - 哪一个更好地使用 JavaScript 中的 new Set()?
问题描述
const array = [1, 2, 2, 2, 3, 4, 4, 4, 4, 5]
// first case
const filteredArray1 = new Set([...array])
// second case
const filteredArray2 = [...new Set(array)]
console.log(filteredArray1, filteredArray2)
解决方案
const filteredArray2 = [...new Set(array)]
console.log(filteredArray1, filteredArray2)
// This is much suitable way of doing it since you'll be directly getting the type of the new variable as an array. In the first method you have the chance of getting the type as a set or an object.
推荐阅读
- c++ - vkCreateSwapchainKHR:内部drawable创建失败
- python - 从 njitted main() 调用 njitted 函数
- binary-tree - 在 isabelle 中哪里可以找到二叉树的定义以及与它们相关的算法?
- c# - 如何避免多次 db 调用以在 EF Core 中插入具有相关实体的对象
- java - 有什么想法可以禁用 hystrix 指标吗?
- python - 在“for循环”中修改列表值
- laravel - Laravel 发送邮件收到 421
- python - 如何扩展字典以包含所有匹配值?
- javascript - 从 ajax 方法返回数据返回状态 400 错误请求
- python - 如何在python的Scrapy中为爬虫设置xpath