var arr=[
{id:1,url:'https://www.baidu.com'},
{id:2,url:'http://www.baidu.com'},
{id:3,url:'http://www.baidu.com/'},
{id:4,url:'https://www.baidu.com/'},
{id:5,url:'https://www.google.com'}
];
如上:
数组中有以上5个对象,id可无视。
业务需求是去重:(以下几种情况)
1.协议不同 内容相同
2.协议相同 地址相同 后缀多/
3.协议不同 内容相同 后缀多/
如用于判断即可用find()遍历
了解:item:当前循环的项。 index:循环项的下标 。 arr:当前循环的数组。 thisvalue:如果传
的话,在callbck函数中this为该值,不穿的话this为undefined。
let isHas=null;
list.find((item)=>{item.url.replace(/http[s]?:\/\//, '')
.replace(/\/$/, "")==$('.addFocus').val()
.replace(/http[s]?:\/\//, '').replace(/\/$/, "")?isHas=true:''})
数组去重
let result = [];
let obj = {};
for(let i = 0; i < data.length; i++){
if(!obj[ data[i].url.replace(/http[s]?:\/\//, '').replace(/\/$/, "")]){
result.push( data[i]);
obj[ data[i].url.replace(/http[s]?:\/\//, '').replace(/\/$/, "")]=true;
}
}
这里可以用replace去掉协议以及最后/在进行比较
replace不会改变原数组的值
result 最后得到的就是去重后的数组
以上数组去重后得到的长度为2的数组
[
{id:1,url:'https://www.baidu.com'},
{id:5,url:'https://www.google.com'}
]
关于对象数组去重以及判断url
最新推荐文章于 2024-08-25 09:57:22 发布
关键词由CSDN通过智能技术生成