当前位置:网站首页>JS two methods to determine whether there are duplicate values in the array
JS two methods to determine whether there are duplicate values in the array
2022-06-11 06:26:00 【Э Time walker to me】
Determine if there are duplicate values in the array , If it is repeated, it will prompt that the priority is repeated

The first method recommend
// Array value as key , Continue adding values to an object ,
// If you find that this attribute already exists in the object before adding value , Then he is repeating
function isRepeat(arr) {
var hash = {
}
for (var i in arr) {
if (hash[arr[i]]) {
return true }
hash[arr[i]] = true
}
return false
},
All the code
<Input v-model.trim="item.haha" @on-blur="func1" clearable placeholder=" Please enter " :style="{ width: formInputWidth1 }"></Input>
// Array value as key , Continue adding values to an object ,
// If you find that this attribute already exists in the object before adding value , Then he is repeating
isRepeat(arr) {
var hash = {
}
for (var i in arr) {
if (hash[arr[i]]) {
return true }
hash[arr[i]] = true
}
return false
},
func1() {
let list = []
this.form2.items.forEach(item => {
// this.form2.items It's my data
list.push(item.haha) // item.haha Is the data I want to check for duplicates
})
let temp = this.isRepeat(list) // If there is a repetition, it returns true, Otherwise false
// let temp = this.arrHasvalue(list) The second method is not recommended , There is a problem
if (temp) {
this.$Message.error({
content: ' Priority has duplicate , Please check !', duration: 3 })
}
console.log(list, temp, ' priority ', this.$cloneDeep(this.form2.items))
},
The second method Not recommended
function arrHasvalue(arr) {
var nary = arr.sort()
for (var i = 0; i < arr.length; i++) {
if (nary[i] == nary[i + 1]) {
console.log(' Array duplicates :' + nary[i])
return true
} else {
return false
}
}
},

边栏推荐
猜你喜欢

Differences between FindIndex and indexof

A multi classification model suitable for discrete value classification -- softmax regression

Simple understanding of pseudo elements before and after

Why don't we have our own programming language?

关于SIoU的原理和代码实现(回顾IoU、GIoU、DIoU、CIoU)

解决ffmpeg获取AAC音频文件duration不准

Convert text label of dataset to digital label

山东大学项目实训之examineListActivity

Deployment of Flink

CCS method of installing compiler
随机推荐
FPGA interview notes (II) -- synchronous asynchronous D flip-flop, static and dynamic timing analysis, frequency division design, retiming
instanceof到底是怎样判断引用数据类型的!
Topic collection of FIFO minimum depth calculation
Vulnhub's breach1.0 range exercise
Shuffleerror:error in shuffle in fetcher solution
Sharing of personal common software and browser plug-ins
Installing and using sublist3r in Kali
Error code in ijkplayer
Growth Diary 01
Handwritten promise [01] - Implementation of promise class core logic
Review Servlet
jenkins-凭证管理
Alias the path with the help of craco
Multimedia框架解析之MediaExtractor源码分析(一)
Teach everyone how to implement an electronic signature
021-MongoDB数据库从入门到放弃
Detailed steps for installing mysql-5.6.16 64 bit green version
CCS method of installing compiler
山东大学项目实训之examineListActivity
Deployment of Flink