当前位置: 首页 > news >正文

长治制作网站百度站长

长治制作网站,百度站长,自己做炉石卡牌的网站,营销网站建设套餐数组去重有许多种方法,下面列举几种常见方法 数组去重数组对象去重 数组去重 使用 Set:将数组转化为 Set 对象,去重后再转化回数组,Set 会自动去重 const arr [1, 2, 3, 2, 1, 4] const newArr [...new Set(arr)] console.log…

数组去重有许多种方法,下面列举几种常见方法

    • 数组去重
    • 数组对象去重

数组去重

  1. 使用 Set:将数组转化为 Set 对象,去重后再转化回数组,Set 会自动去重
const arr = [1, 2, 3, 2, 1, 4]
const newArr = [...new Set(arr)]
console.log(newArr) // [1, 2, 3, 4]
  1. 使用 filter:遍历数组,对每个元素判断是否在新数组中出现过。
const arr = [1, 2, 3, 2, 1, 4]
const newArr= arr.filter((item, index) => {return arr.indexOf(item) === index
})
console.log(newArr) // [1, 2, 3, 4]
  1. 使用 reduce:遍历数组,对每个元素判断是否在新数组中出现过,如果没有则将其添加到新数组中。
const arr = [1, 2, 3, 2, 1, 4]
const newArr= arr.reduce((acc, cur) => {if (!acc.includes(cur)) {acc.push(cur)}return acc
}, [])
console.log(newArr) // [1, 2, 3, 4]
  1. 使用 Map:遍历数组,将每个元素作为 key 存储到 Map 中,去重后再转化回数组。
const arr = [1, 2, 3, 2, 1, 4]
const map = new Map()
arr.forEach((item) => {map.set(item, true)
})
const newArr= Array.from(map.keys())
console.log(newArr) // [1, 2, 3, 4]

需要注意的是,以上方法都无法去重包含对象、数组等引用类型的元素的数组,需要使用其他方法实现。另外,以上方法去重后的数组顺序可能与原数组不同,如果需要保持顺序可以使用其他方法,比如通过遍历原数组将不重复的元素依次添加到新数组的尾部。

数组对象去重

  1. 使用 Set :Set 是 ES6 中新增的一种数据结构,它类似于数组,但是成员的值都是唯一的,可以用来去重。我们可以使用 Set 来去重数组对象,然后再将结果转换为数组。
const arr = [{ id: 1, name: 'AAAA' },{ id: 2, name: 'BBBB' },{ id: 1, name: 'AAAA' },{ id: 3, name: 'CCCC' }
];const result = Array.from(new Set(arr.map(JSON.stringify)), JSON.parse);
console.log(result); // [{ id: 1, name: 'AAAA' }, { id: 2, name: 'BBBB' }, { id: 3, name: 'CCCC' }]
  1. 使用 reduce :我们也可以使用 reduce 方法进行去重,具体步骤如下:
    · 遍历数组中的每一个元素;
    · 对于每一个元素,判断它是否已经出现过(使用 Array.prototype.findIndex() 判断);
    · 如果没有出现过,就将它添加到结果数组中;
const arr = [{ id: 1, name: 'AAAA' },{ id: 2, name: 'BBBB' },{ id: 1, name: 'AAAA' },{ id: 3, name: 'CCCC' }
]const result = arr.reduce((acc, curr) => {const index = acc.findIndex((item) => item.id === curr.id)if (index < 0) {acc.push(curr)}return acc
}, [])console.log(result) // [{ id: 1, name: 'AAAA' }, { id: 2, name: 'BBBB' }, { id: 3, name: 'CCCC' }]
  1. 使用 Map : Map 也可以用来去重数组对象,具体步骤如下:
    · 遍历数组中的每一个元素;
    · 对于每一个元素,判断它是否已经出现过(使用 Map.has() 判断);
    · 如果没有出现过,就将它添加到结果数组中;
const arr = [{ id: 1, name: 'AAAA' },{ id: 2, name: 'BBBB' },{ id: 1, name: 'AAAA' },{ id: 3, name: 'CCCC' }
]const map = new Map()
const result = []for (const item of arr) {if (!map.has(item.id)) {map.set(item.id, true)result.push(item)}
}console.log(result) // [{ id: 1, name: 'AAAA' }, { id: 2, name: 'BBBB' }, { id: 3, name: 'CCCC' }]
http://www.dinnco.com/news/33678.html

相关文章:

  • 企业网站建设教程视频seo免费系统
  • 重庆微信网站开发中国教育培训网
  • 慈溪网站建设报价电子商务
  • 小型网站开发 论文韶山百度seo
  • 做网站的小图标优化20条措施
  • 网站建设岗位旅游新闻热点
  • 做定制型网站互联网营销师培训教程
  • 微信商城源码快速排名seo
  • 网站开发通用流程图郑州seo推广优化
  • 打字做任务赚钱的网站seo外链建设的方法
  • 视屏网站制作什么是精准营销
  • 一件代发50个货源网站山东网络推广网站
  • 网站部署电商关键词一般用哪些工具
  • 网站开发与桌面应用开发百度成都总部
  • 成都网站建设培训学校搜索引擎推广法
  • 有女人和马做网站吗专注于品牌营销服务
  • 公司网站建设30元百度seo排名主要看啥
  • 做网站协议书网络营销推广的方法有哪些
  • 网页传奇新开网站网络营销与策划试题及答案
  • 网站选择理由描述外贸独立站怎么建站
  • 我wordpress top主题上海站群优化
  • 有限公司 wordpress电商seo什么意思
  • wordpress怎么发布文章带图片seoul
  • 专业建站公司收费标准百度快速收录网站
  • 寻找在山西运城专业做网站推广的营销网
  • 网站无法做301重定向安卓aso
  • h5响应式网站源码百度站内搜索的方法
  • 移动路由器做网站服务器上海网络推广招聘
  • 做资质去哪个网站填资料南通seo网站优化软件
  • 厦门的网站建设公司哪家好四年级的简短新闻播报