当前位置:网站首页>JS 数组去重(含简单数组去重、对象数组去重)
JS 数组去重(含简单数组去重、对象数组去重)
2022-08-01 22:18:00 【朝阳39】
简单数组去重
简单数组:由基础数据类型元素组成的数组,如 [1,‘你好’,true]
方式一 : 利用Set去重
利用 Set 数据结构自动去重的特征实现
let oldList = [1, 2, 3, 3];
let newList = Array.from(new Set(oldList)); // 得到 [1, 2, 3]
类似写法如下:
function arrDistinct(arr){
const newArr = new Set(arr);
return [...newArr]
}
let list = [1,2,2,3,4,4]
console.log(arrDistinct(list))
方式二 : 嵌套循环,自己与自己对比,有重复则删掉
let arr = [1, 2, 3, 2, 2, 1, 3, 4, 2, 5];
//去除数组中重复的数字
for (let i = 0; i < arr.length; i++) {
/*获取当前元素后的所有元素*/
for (let j = i + 1; j < arr.length; j++) {
//判断两个元素的值是否相等
if (arr[i] == arr[j]) {
//如果相等则证明出现了重复的元素,则删除j对应的元素
arr.splice(j, 1);
//当删除了当前j所在的元素以后,后边的元素会自动补位
//使j自减,再比较一次j所在位置的元素
j--;
}
}
}
console.log(arr); // 结果为 [ 1, 2, 3, 4, 5 ]
方式三 : 将不重复的元素放入新数组
//思路:创建一个新数组,循环遍历,只要新数组中有老数组的值,就不用再添加了。
function arrDistinct(array) {
let newArr = [];
for (let i = 0; i < array.length; i++) {
//开闭原则
let bool = true;
//每次都要判断新数组中是否有旧数组中的值。
for (let j = 0; j < newArr.length; j++) {
if (array[i] === newArr[j]) {
bool = false;
}
}
if (bool) {
newArr[newArr.length] = array[i];
}
}
return newArr;
}
let oldList = [1, 2, 3, 4, 5, 2, 3, 4];
let newList = arrDistinct(oldList);
console.log(newList); // 结果 [ 1, 2, 3, 4, 5 ]
方式四 : 使用 lodash 的 uniq()
_.uniq([2, 1, 2]); // => [2, 1]
对象数组去重
对象数组:由对象组成的数组
function arrDistinctByProp(arr,prop){
//只返回第一次出现的,重复出现的过滤掉
return arr.filter(function(item,index,self){
return self.findIndex(el=>el[prop]==item[prop])===index
})
}
let list = [
{
key:'1',
name:'林青霞'
},
{
key:'2',
name:'张三丰'
},
{
key:'1',
name:'段誉'
},
{
key:'1',
name:'段誉'
}
]
//通过name属性去重
console.log(arrDistinctByProp(list,'name'))
另一种可能更容易理解,写法如下:
function soloArray(array, UID) {
let newArr = [];
let UIDlist = [];
array.forEach((item) => {
if (!UIDlist.includes(item[UID])) {
UIDlist.push(item[UID]);
newArr.push(item);
}
});
return newArr;
}
let oldList = [
{
ID: 1,
name: "王小虎",
age: 10,
},
{
ID: 2,
name: "张三",
age: 20,
},
{
ID: 2,
name: "张三",
age: 20,
},
{
ID: 3,
name: "李四",
age: 30,
},
];
let newList = soloArray(oldList, "ID");
console.log(newList);
边栏推荐
猜你喜欢

迁移学习——Discriminative Transfer Subspace Learning via Low-Rank and Sparse Representation

一种灵活的智能合约协作方式

Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function

JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法

The must-have "fishing artifact" for programmers is here!

Go 微服务开发框架DMicro的设计思路

使用Jenkins做持续集成,这个知识点必须要掌握

_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined

seaborn笔记:可视化统计关系(散点图、折线图)

SOM Network 1: Principles Explained
随机推荐
SRv6 L3VPN的工作原理
联邦学习入门
Getting Started Database Days4
[ASM] Bytecode Operation MethodWriter
使用 Zokrates 在 BSV 上创建您的第一个 zkSNARK 证明
Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
不卷了!入职字节跳动一周就果断跑了。
小程序毕设作品之微信美食菜谱小程序毕业设计成品(7)中期检查报告
Deep learning Course2 first week Practical aspects of Deep Learning exercises
NgRx Store createSelector 的单步调试和源代码分析
[Mobile Web] Mobile terminal adaptation
PHP算法之最接近的三数之和
Go 微服务开发框架DMicro的设计思路
KMP 字符串匹配问题
Deep Learning Course2 Week 2 Optimization Algorithms Exercises
复现gallerycms字符长度限制短域名绕过
Kubernetes Scheduler全解析
SAP ABAP OData 服务如何支持删除(Delete)操作试读版
2022年最新河北建筑八大员(机械员)模拟考试题库及答案
联邦学习在金融领域的发展和应用