hash 筛选,去重
来源:互联网 发布:新世界 电影 知乎 编辑:程序博客网 时间:2024/05/22 00:52
因为工作需要,遇到两个对象数组A,B,找到A中B不存在的对象,并保存在一个数组中,之前一直采用正常的for循环的方式,每次找到一个便删掉的方式,比较麻烦,而且不是太好。先采用hash数组的方法来解决。
var roles=[{"des":"t04_Cm","name":"测试4","pId":null,"id":52}]; var allNodes=[{"noR":true,"iconOpen":"common/css/img/open.png","name":"类列表","pId":0,"id":0,"iconClose":"common/css/img/close.png","open":true},{"iconOpen":"common/css/img/open.png","des":"test01_Cm","name":"测试1","tableid":47,"pId":0,"id":47,"iconClose":"common/css/img/close.png","open":true},{"iconOpen":"common/css/img/open.png","des":"test02_Cm","name":"测试2","tableid":48,"pId":0,"id":48,"iconClose":"common/css/img/close.png","open":true},{"iconOpen":"common/css/img/open.png","des":"test03_Cm","name":"测试3","tableid":49,"pId":0,"id":49,"iconClose":"common/css/img/close.png","open":true},{"iconOpen":"common/css/img/open.png","des":"t04_Cm","name":"测试4","tableid":52,"pId":0,"id":52,"iconClose":"common/css/img/close.png","open":true}]; var rest=[],hash=[];
var roles=[{"des":"t04_Cm","name":"测试4","pId":null,"id":52}]; var allNodes=[{"noR":true,"iconOpen":"common/css/img/open.png","name":"类列表","pId":0,"id":0,"iconClose":"common/css/img/close.png","open":true},{"iconOpen":"common/css/img/open.png","des":"test01_Cm","name":"测试1","tableid":47,"pId":0,"id":47,"iconClose":"common/css/img/close.png","open":true},{"iconOpen":"common/css/img/open.png","des":"test02_Cm","name":"测试2","tableid":48,"pId":0,"id":48,"iconClose":"common/css/img/close.png","open":true},{"iconOpen":"common/css/img/open.png","des":"test03_Cm","name":"测试3","tableid":49,"pId":0,"id":49,"iconClose":"common/css/img/close.png","open":true},{"iconOpen":"common/css/img/open.png","des":"t04_Cm","name":"测试4","tableid":52,"pId":0,"id":52,"iconClose":"common/css/img/close.png","open":true}]; var rest=[],hash=[]; $.each(roles,function(key,val){ hash[val.id]=true; }) $.each(allNodes,function(index,value){ if(hash[value.id]===undefined){ rest.push(value); } }); console.log(rest)
再介绍一种hash数组去重的方法,可以将数组中的相同项删掉。
function unique2(arr){ //i遍历arr,同时创建两个空数组result和hash for(var i=0,result=[],hash=[]; i<arr.length; i++){ //如果hash中以当前元素为key的元素是undefined if(hash[arr[i]]===undefined){ //将当前元素追加到result结尾 result[result.length]=arr[i]; //在hash中添加一个新元素: key为当前元素值,值为true hash[arr[i]]=true; } }//(遍历结束) return result;//返回result}
阅读全文
0 0
- hash 筛选,去重
- webjs--hash数组去重
- 自定义hash高效去重
- EXCEL 一组数据筛选出重复的数据/去重
- hlg1287数字去重和排序II【hash】
- Guava ImmutableSet源码去重处理,Hash处理
- Python 基于Hash算法对海量文件去重方案
- hash去重及应用-【leetcode219-Contains Duplicate II】
- JavaScript使用hash表方式实现数组去重。
- 去重
- 去重
- 去重
- 去重
- 去重
- ZOJ Monthly, July 2015 K-hash 后缀数组去重做法
- 两道前端面试题目:1.变量作用域,2.利用hash将数组去重
- 哈理工oj Hrbustacm 1287 数字去重和排序II(STL 或着 hash 拉链法)
- 数组去重,json数组去重
- 小技巧小知识——Python
- 如何用MDK从零开始创建STM32F2xx工程(图文教程)
- 腾讯视频下载的qlv格式转换为MP4格式
- opencv——同一窗口显示同色彩图像
- Web前端开发回顾之javaScript(1)
- hash 筛选,去重
- spark性能优化:数据倾斜调优
- Spring Boot实战之netty-socketio实现简单聊天室(给指定用户推送消息)
- html标签之头部元素
- MobaXterm中运行脚本出现command not found
- Struts2笔记02 Struts2中result配置中各种视图转发类型
- 图像序列合成视频(vs2013+opencv)
- KeePass:强大的开源免费放心的密码管理器
- Storm安装与启动