Find duplicate array elements

Find duplicate array elements

indexOf & lastIndexOf (this string may be used, does not need to sort)

  • See previous number (the indexOf) and the number (lastIndexOf) are inconsistent after subscript
  • To see if there is an array of arrRepeat had not existed, then to push into
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
}

Guess you like

Origin blog.csdn.net/u011121176/article/details/90238722