前端数组去重

function unique (arr) {
  return Array.from(new Set(arr))
}
var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
console.log(unique(arr))
//[1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {}, {}]
复制代码
export function arrayUnique(songs) {
    const result = {};
    const finalResult = [];
    for (let i = songs.length - 1; i >= 0; i--) {
        result[songs[i].name] = songs[i];
    }
    Object.keys(result).map((item) => finalResult.push(result[item]));
    return finalResult;
}
复制代码
复制代码
function unique(arr){            
  
for(var i=0; i<arr.length; i++){   for(var j=i+1; j<arr.length; j++){   if(arr[i]==arr[j]){ //第一个等同于第二个,splice方法删除第二个     arr.splice(j,1);   j--;       }     }   }   return arr; }
复制代码

 

posted on   忘忧很努力呀~  阅读(138)  评论(0编辑  收藏  举报

编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 赶AI大潮:在VSCode中使用DeepSeek及近百种模型的极简方法
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

统计

点击右上角即可分享
微信分享提示