js数组操作方法总结(必看篇)
程序员文章站
2022-05-18 13:12:10
...
判断数值数组中各个数字出现的奇偶次数
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>数组操作</title> </head> <body> <script type="text/javascript"> var arr=[3,1,2,2,1,3,1]; var sum=[]; var res=[]; var count=0; var temp; for(var i=0;i<arr.length;i++){ if(res.indexOf(arr[i])==-1){ res.push(arr[i]); } } for(var i=0;i<res.length;i++){ for(var j=0;j<arr.length;j++){ if(arr[j]==res[i]){ count++; } } sum.push(count); count=0; } console.log(res);//[3,1,2] for(var i=0;i<res.length;i++){ var str=(sum[i]%2==0)?"偶数":"奇数"; console.log(res[i]+"出现了"+sum[i]+"次"); console.log(res[i]+"出现了"+str+"次"); } </script> </body> </html>
阿里笔试-数组操作-找出两个数组中不同的元素
<script type="text/javascript"> function diff(arr1,arr2){ var ress = []; var arr = arr1.concat(arr2); for(var i=0,len=arr.length;i<len;i++){ if((arr1.indexOf(arr[i])>=0 && arr2.indexOf(arr[i])<0) || (arr1.indexOf(arr[i])<0 && arr2.indexOf(arr[i])>=0)){ ress.push(arr[i]); } } return ress; } var arr1 = [1,2,3,5,7,6]; var arr2 = [1,2,5]; var res = diff(arr1,arr2); console.log(res);//[3, 7, 6] </script>
数组去重
方法1
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>数组去重01</title> </head> <body> <script type="text/javascript"> //给数组原型添加方法 Array.prototype.unique = function(){ var arr = []; for(var i=0,i=this.length;i<len;i++){ if(arr.indexOf(this[i]) == -1){ arr.push(this[i]); } } return arr; }; console.log([1,2,3,2,5,6,3].unique());//[1, 2, 3, 5, 6] </script> </body> </html>
方法2
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>数组去重02</title> </head> <body> <script type="text/javascript"> Array.prototype.unique = function(){ var n = {}, r=[]; //n为哈希表,r为临时数组 for(var i = 0; i < this.length; i++) //遍历当前数组 { if (!n[this[i]]) //如果hash表中没有当前项 { n[this[i]] = true; //存入哈希表 r.push(this[i]); //把当前数组的当前项push到临时数组里面 } } return r; } console.log([1,2,3,2,5,6,3].unique());//[1, 2, 3, 5, 6] </script> </body> </html>
方法3
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>数组去重</title> </head> <body> <script type="text/javascript"> Array.prototype.unique = function(){ var arr = [this[0]];//结果数组 for(var i=1;i<this.length;i++){//从第二项开始遍历 if(this.indexOf(this[i]) == i){ //如果当前数组的第i项在当前数组中第一次出现的位置不是i,那么表示第i项是重复的,忽略掉。否则存入结果数组 arr.push(this[i]); } } return arr; } console.log([1,2,3,4,2,3,4].unique());// [1, 2, 3, 4] </script> </body> </html>
方法4
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>filter</title> <link rel="stylesheet" href=""> </head> <body> <script type="text/javascript"> var arr = [4,5,3,2,3,4,5,1]; function fn(num){ var res = num.filter(function(item,index,array){ return num.indexOf(item) === index;//num.indexOf(item)将会返回元素在数组第一次出现的位置 //对于多次出现的元素,除第一次外,其他情况都返回false }); return res; } console.log(fn(arr));//[4, 5, 3, 2, 1] </script> </body> </html>
方法5
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Object 对象来当做哈希表</title> <link rel="stylesheet" href=""> </head> <body> <script type="text/javascript"> function unique(a) { var obj = {}; return a.filter(function(item) {//filter会返回true的项组成的成员 return obj.hasOwnProperty(item) ? false : (obj[item] = true); }); } var a = [1, 1, 3, 2, 1, 2, 4]; var res = unique(a); console.log(res); // [1, 3, 2, 4] </script> </body> </html>
下一篇: 类似 nginx 编译时生成函数链表