js determines whether there are duplicate arrays in an array/whether a certain attribute value of an object in an array is duplicated

In projects, we often encounter whether there is a certain similar value in array processing. Or whether there are identical elements in the object...
The following methods are commonly used, but are not limited to.

1. Ordinary array data
1.1 Sort the array and compare whether the previous element and the next element are equal. If they are equal, it means that the array has duplicate values.

const arr=['111','222','333','444','555'];
//判断数组元素是否有重复
var sarr = arr.sort();
for(let i=0; i<sarr.length; i++){
    if(sarr[i] == sarr[i+1]){
        alert("数组中重复元素是:" + sarr[i])
    }
}

Insert image description here
1.2: First convert the array into a string, and then traverse the array. If you can still find the element after removing the current array element from the string, it means that the array has duplicate values.

	const arr=['111','111','333','444','555'];
	var str = arr.join(",") + ",";
	for(let i=0; i<arr.length; i++){
	    if(str.replace(arr[i]+",", "").indexOf(arr[i]+",") > -1){
	        alert("数组中重复元素是:" + arr[i]);
	        break
	    }
	}

Insert image description here
1.3 First use ES6 syntax to deduplicate the array, and then compare the length with the original array. If the length is smaller than the original array, it means that the array has duplicate values.
Array.from(new Set(arr)).length < arr.length

//ES6去重方法
// 1. 拓展运算符 + new Set方法

const arr=['111','222','333','444','555'];
let arr1 = [...new Set(arr)]
if(arr2.length < arr.length) {
	console.log('有重复')
}else{
	console.log('没有重复')
}

Insert image description here

	// 2. Array.from + new Set方法
	const arr=['111','111','333','444','555'];
	let arr2 = Array.from(new Set(arr))
	if(arr2.length < arr.length) {
		console.log('有重复')
	}else{
		console.log('没有重复')
	}

Insert image description here

2. Object element array
Requirement: To determine whether there is duplicate data in name/age in the array below.

2.1. First use the ES6 syntax Set to deduplicate the array, and then compare the length of the original array. If the length is less than the original array, it means that the array has duplicate values.

	const arr=[
	  {name:"张三3",age:12},
	  {name:"张三2",age:12},
	  {name:"张三",age:12},
	  {name:"张三1",age:12}
	];
	const newListLength=new Set(arr.map(item=>item.name)).size;
	const listLength=arr.length;
	if(listLength>newListLength){
	  console.log("重复");
	}

Insert image description here

2.2. First convert the array into a string, and then traverse the array. If you can still find the element after removing the current array element from the string, it means that the array has duplicate values.

const arr=[
  {name:"张三3",age:12},
  {name:"张三2",age:12},
  {name:"张三",age:12},
  {name:"张三1",age:12}
];
/*
  replace不用正则的话,只会替换掉第一个符合条件的数据
  之所以加逗号,是因为这样可以确保数据的唯一性,如果不加逗号,会导致数据不对,比如说第三条数据"张三",replace之后还会查到第四条数据中的"张三",所以得加上逗号确保唯一性
*/
const newArr=arr.map(item=>item.name);
const str=newArr.join(",")+",";
const flag=newArr.some(item=>{
  return str.replace(item+",","").indexOf(item+",")>-1
});
if(flag){
  console.log("重复");
}

Insert image description here
2.3. Check whether the subscript found using findIndex or indexOf is equal to the subscript of the current loop.

	//indexOf查找是否有重复的
	const arr=[
	  {name:"张三3",age:12},
	  {name:"张三2",age:12},
	  {name:"张三",age:12},
	  {name:"张三1",age:12}
	];
	const newArr=arr.map(item=>item.name);
	const isRepeat=newArr.some((item,index,arr)=>arr.indexOf(item)!=index);
	if(isRepeat){
	  console.log("重复");
	}

Insert image description here

	//findIndex查找是否有重复的
	const arr=[
	  {name:"张三3",age:12},
	  {name:"张三2",age:12},
	  {name:"张三",age:12},
	  {name:"张三1",age:12}
	];
	const newArr=arr.map(item=>item.name);
	const isRepeat=newArr.some((x,index,arr)=>arr.findIndex(y=>y==x)!=index);
	if(isRepeat){
	  console.log("重复");
	}

Insert image description here

Guess you like

Origin blog.csdn.net/lzfengquan/article/details/128816140