Js: Get repeated attribute values of array objects and deduplication of array objects

Array deduplication

Deduplication of object arrays is divided into two categories: deduplication based on a certain attribute, which is exactly the same object as deduplication (the attribute and attribute values ​​are the same)
1. Array nested objects, deduplication based on a certain attribute of the object

let arr = [
	{
    
    id:1, setting:'demo', jointCategoryName:'success'},
	{
    
    id:2, setting:'dev', jointCategoryName:'success'},
	{
    
    id:3, setting:'prod', jointCategoryName:'fail'},
	{
    
    id:4, setting:'demo', jointCategoryName:'waiting'},
	{
    
    id:3, setting:'prod', jointCategoryName:'fail'},
	{
    
    id:2, setting:'test', jointCategoryName:'success'}
]
function unipFunc(arr){
    
    
	let arr1 = []; //存id
	let newArr = []; //存新数组
	for(let i in arr){
    
    
		if(arr1.indexOf(arr[i].id) == -1){
    
    
			arr1.push(arr[i].id);
			newArr.push(arr[i]);
		}
	}
	return newArr;
}

2. Array nested objects, duplicate identical objects (property and attribute values ​​are all the same)

The arrangement is as follows:
First, loop through the array to get an array composed of all properties of the object;
secondly, loop through the property array to splice the properties of the object and the corresponding values ​​into a string;
then, use the hasOwnProperty method to determine whether the string is a property in the object obj, if not, use this string as a property, and add a new property to the obj object as a value of true;

let arr = [
	{
    
    id:1, setting:'demo', jointCategoryName:'success'},
	{
    
    id:2, setting:'dev', jointCategoryName:'success'},
	{
    
    id:3, setting:'prod', jointCategoryName:'fail'},
	{
    
    id:4, setting:'demo', jointCategoryName:'waiting'},
	{
    
    id:3, setting:'prod', jointCategoryName:'fail'},
	{
    
    id:2, setting:'test', jointCategoryName:'success'}
]
function unipFunc(arr){
    
    
	var newArr= []; //存新数组
    var obj= {
    
    }; //存处理后转成字符串的对象
    for (var i = 0; i < arr.length; i++) {
    
    
        var keys = Object.keys(arr[i]);
        //keys.sort(function(a, b) {
    
    
        //    return (Number(a) - Number(b));
        //});
        var str = '';
        for (var j = 0; j < keys.length; j++) {
    
    
            str += JSON.stringify(keys[j]);
            str += JSON.stringify(arr[i][keys[j]]);
        }
        if (!obj.hasOwnProperty(str)) {
    
    
            newArr.push(arr[i]);
            obj[str] = true;
        }
    }
    return newArr;
}

3. Ordinary array deduplication

function unipFunc(arr) {
    
    
    let newArr = [];
    for (var i = 0; i < arr.length; i++) {
    
    
         if (newArr.indexOf(arr[i]) === -1) {
    
    
                newArr.push(arr[i]);
         }
    }
    return newArr;
}

4. Ordinary arrays get repeated elements

方法一:indexOf和lastIndexOf
function unipFunc(arr) {
    
    
    let newArr = [];
    arr.forEach((item)=>{
    
    
        if(arr.indexOf(item) !== arr.lastIndexOf(item) && newArr.indexOf(item) === -1){
    
    
            newArr.push(item);
        }
    });
    return newArr;
}
方法二:双层for循环
function unipFunc(arr) {
    
    
    var Arr = [];
    for(let i=0; i<arr.length; i++ ){
    
    
        for(let j=i+1; j<arr.length; j++){
    
    
            if(arr[i]===arr[j] && Arr.indexOf(arr[j])===-1){
    
    
                Arr.push(arr[i]);
            }
        }
    }
    return Arr;
}

5. Array nested objects, get repeated elements and unique elements and coordinates

//数据
const arr =[
    {
    
    id:1, schoolId:'100', jointCategoryName:'美术'},
	{
    
    id:2, schoolId:'200', jointCategoryName:'美术'},
	{
    
    id:3, schoolId:'300', jointCategoryName:'设计'},
	{
    
    id:4, schoolId:'100', jointCategoryName:'音乐'},
	{
    
    id:3, schoolId:'300', jointCategoryName:'设计'},
	{
    
    id:2, schoolId:'400', jointCategoryName:'美术'}
]
  let key = {
    
    } //存储的 key 是type的值,value是在indeces中对应数组的下标
  let indices = [] //数组中每一个值是一个数组,数组中的每一个元素是原数组中相同type的下标 
  arr.map((item, index) => {
    
    
    //根据对应字段 分类(type)
    let type= item.type
    let _index = key[type]
    if (_index !== undefined) {
    
    
      indices[_index].push(index)
    } else {
    
    
      key[type] = indices.length
      indices.push([index])
    }
  })
  // 归类结果
  let result = []
  indices.map((item) => {
    
    
    item.map((index) => {
    
    
    //result.push(List[index]) 相同项排序在一起
    //if (item.length > 1) {} 只要重复项
    //if (item.length == 1){} 只要单独项
  
    //我这里需要重复项 根据业务处理
    if (item.length > 1) {
    
    
      result.push(arr[index])
    }
  })
  })
  console.log('获取重复的值=====================>',result[0].type)
  return result;

6. Map()

let arrObj = [
    {
    
    id:1, schoolId:'100', jointCategoryName:'美术'},
	{
    
    id:2, schoolId:'200', jointCategoryName:'美术'},
	{
    
    id:3, schoolId:'300', jointCategoryName:'设计'},
	{
    
    id:4, schoolId:'100', jointCategoryName:'音乐'},
	{
    
    id:3, schoolId:'300', jointCategoryName:'设计'},
	{
    
    id:2, schoolId:'400', jointCategoryName:'美术'}
];
// 方法一:
let map = new Map();
for (let item of arrObj) {
    
    
    if (!map.has(item.id)) {
    
    
        map.set(item.id, item);
    };
};
arr = [...map.values()];
console.log(arr);
 
 
 
// 方法二: (代码较为简洁)
const map = new Map();
const newArr = arrObj.filter(v => !map.has(v.id) && map.set(v.id, 1));
console.log(newArr);

7. Array deduplication
Normal array deduplication

let arr = [1, 1, 1, 2, 3, 4, 5, 5, 6];
let crr = [];
const res = arr.reduce((brr, va) => {
    
    
  if (!brr.includes(va)) {
    
    
    brr.push(va);
  } else {
    
    
    crr.push(va);
  }
  return brr;
}, []);
console.log(res);
console.log(crr);

Array Object Deduplication
Method 1

let arr = [{
    
    label:'你好',prop:'aa'},{
    
    label:'你好',prop:'aa'},{
    
    label:'你好2',prop:'bb1'},{
    
    label:'你好2',prop:'bb2'}]
let deWeightArr = []; // 去重后的数组
let repetitionArr  = []; // 重复的数组
arr.forEach((item1) => {
    
    
  const check = deWeightArr.every((item2) => {
    
    
    return item1.prop !== item2.prop;
  });
  check ? deWeightArr.push(item1) : repetitionArr.push(item1);
});
console.log('重复的数据================>>>>>',repetitionArr)
console.log('获取数组去重后的数据================>>>>>',deWeightArr)

Method Two

  this.repeatData = [];
  dataList.forEach((item) => {
    
    
    if (
      this.repeatData.filter((material) => material.archiveRank === item.archiveRank)
        .length === 0
    ) {
    
    
      this.repeatData.push(item);
    }
  });
  console.log('获取重复的数组数据=====》<<<<<<<<<<<<<<<<<<<<<<<',this.repeatData)

Native deduplication

let arr = 
[
   	{
    
    id:1, schoolId:'100', jointCategoryName:'美术'},
	{
    
    id:2, schoolId:'200', jointCategoryName:'美术'},
	{
    
    id:3, schoolId:'300', jointCategoryName:'设计'},
	{
    
    id:4, schoolId:'100', jointCategoryName:'音乐'},
	{
    
    id:3, schoolId:'300', jointCategoryName:'设计'},
	{
    
    id:2, schoolId:'400', jointCategoryName:'美术'}
];
export const distinct = (arr) => {
    
    
  var result = [];
  var obj = {
    
    };
  for (var i = 0; i < arr.length; i++) {
    
    
    if (!obj[arr[i].jointCategoryName]) {
    
    
      result.push(arr[i]);
      obj[arr[i].jointCategoryName] = true;
    }
  }
  console.log('获取重复的数组数据=====》<<<<<<<<<<<<<<<<<<<<<<<',result)
  return result;
};
distinct(arr)

Guess you like

Origin blog.csdn.net/m0_44973790/article/details/129243258