数组去重
对象数组去重分为两类:根据某一属性去重,和去重完全相同对象(属性属性值都相同)
一.数组嵌套对象,根据对象某一属性去重
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;
}
二、数组嵌套对象,去重完全相同对象(属性属性值都相同)
整理如下:
首先、循环数组,拿到对象的所有属性组成的数组;
其次、循环属性数组把对象的属性和对应的值拼接成字符串;
然后、利用 hasOwnProperty 方法判断这个字符串是不是对象 obj 里的属性,如果不是则以这个字符串为属性,true为值给 obj 对象新增一个属性;
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;
}
三、普通数组去重
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;
}
四、普通数组获取重复元素
方法一: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;
}
五、数组嵌套对象,获取重复元素和唯一元素及坐标
//数据
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;
六、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);
七、数组去重
普通数组去重
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);
数组对象去重
方法一
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)
方法二文章来源:https://www.toymoban.com/news/detail-812201.html
this.repeatData = [];
dataList.forEach((item) => {
if (
this.repeatData.filter((material) => material.archiveRank === item.archiveRank)
.length === 0
) {
this.repeatData.push(item);
}
});
console.log('获取重复的数组数据=====》<<<<<<<<<<<<<<<<<<<<<<<',this.repeatData)
原生去重文章来源地址https://www.toymoban.com/news/detail-812201.html
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)
到了这里,关于Js:获取数组对象重复属性值和数组对象去重的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!