标签:js 数组元素对比
最近经常遇到js两个数组比较元素差别,特此记录方便以后查找。
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> <title>测试</title> <script type="text/javascript"> var a1 = [1,3,4,5,6]; var a2 = [2,3,7]; var shao = []; var duo = []; var hebing = a1.concat(a2); hebing.sort(); hebing.reverse(); var quchong = $.unique(hebing); console.info(a1); console.info(a2); console.info(quchong); quchong.forEach(function(qitem,index,array){ if(a1.every(function(item,qindex,array){return qitem != item;})) { duo.push(qitem); } if(a2.every(function(item,qindex,array){return qitem != item;})) { shao.push(qitem); } }); console.info("a2比a1多的元素"+duo); console.info("少的元素"+shao); </script> </head> <body> </body> </html>
本文出自 “GUI” 博客,请务必保留此出处http://xingfudehunpo.blog.51cto.com/1843260/1593998
标签:js 数组元素对比
原文地址:http://xingfudehunpo.blog.51cto.com/1843260/1593998