找出数组中重复的元素

找出数组中重复的元素

indexOf & lastIndexOf(这个可以用于字符串,不需要sort)

  • 看 从前数(indexOf)与 从后数(lastIndexOf)的下标是否不一致
  • 看看 arrRepeat数组 是否存在过,未存在过的话就 push进去
let arrAll = []
this.bodyParams.detailList.forEach(item => {
  arrAll.push(item.productNo)
})
let arrRepeat = []
let textRepeat = ''
arrAll.forEach(item => {
  if (arrAll.indexOf(item) !== arrAll.lastIndexOf(item) && arrRepeat.indexOf(item) === -1) {
    arrRepeat.push(item)
    textRepeat += `${item} `
  }
})
if (arrRepeat.length > 0) {
  this.$Message.error(`产品编码 ${textRepeat}重复`)
  return
}

猜你喜欢

转载自blog.csdn.net/u011121176/article/details/90238722