判斷數值數組中各個數字出現的奇偶次數
<!DOCTYPE html> <html lang="en"> <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 lang="en"> <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 lang="en"> <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 lang="en"> <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>
以上就是小編為大家帶來的js數組操作方法總結(必看篇)全部內容了,希望大家多多支持~