Determine the number of odd and even numbers in the numeric array
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Array operation</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<;i++){ if((arr[i])==-1){ (arr[i]); } } for(var i=0;i<;i++){ for(var j=0;j<;j++){ if(arr[j]==res[i]){ count++; } } (count); count=0; } (res);//[3,1,2] for(var i=0;i<;i++){ var str=(sum[i]%2==0)?"even":"odd number"; (res[i]+"It appears"+sum[i]+"Second-rate"); (res[i]+"It appears"+str+"Second-rate"); } </script> </body> </html>
Alibaba written test - array operation - find out different elements in two arrays
<script type="text/javascript"> function diff(arr1,arr2){ var ress = []; var arr = (arr2); for(var i=0,len=;i<len;i++){ if(((arr[i])>=0 && (arr[i])<0) || ((arr[i])<0 && (arr[i])>=0)){ (arr[i]); } } return ress; } var arr1 = [1,2,3,5,7,6]; var arr2 = [1,2,5]; var res = diff(arr1,arr2); (res);//[3, 7, 6] </script>
Array deduplication
Method 1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Array deduplication01</title> </head> <body> <script type="text/javascript"> //Add method to array prototype = function(){ var arr = []; for(var i=0,i=;i<len;i++){ if((this[i]) == -1){ (this[i]); } } return arr; }; ([1,2,3,2,5,6,3].unique());//[1, 2, 3, 5, 6] </script> </body> </html>
Method 2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Array deduplication02</title> </head> <body> <script type="text/javascript"> = function(){ var n = {}, r=[]; //n is a hash table, r is a temporary array for(var i = 0; i < ; i++) //Transfer the current array { if (!n[this[i]]) //If there is no current item in the hash table { n[this[i]] = true; //Save hash table (this[i]); //Push the current item of the current array into the temporary array } } return r; } ([1,2,3,2,5,6,3].unique());//[1, 2, 3, 5, 6] </script> </body> </html>
Method 3
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Array deduplication</title> </head> <body> <script type="text/javascript"> = function(){ var arr = [this[0]];//Result array for(var i=1;i<;i++){//Start the second item if((this[i]) == i){ //If the i-th item of the current array appears in the first time in the current array is not i, then it means that the i-th item is repeated and ignored. Otherwise, save the result array (this[i]); } } return arr; } ([1,2,3,4,2,3,4].unique());// [1, 2, 3, 4] </script> </body> </html>
Method 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 = (function(item,index,array){ return (item) === index;//(item) will return the location where the element first appears in the array //For elements that appear multiple times, false will be returned in other cases except for the first time. }); return res; } (fn(arr));//[4, 5, 3, 2, 1] </script> </body> </html>
Method 5
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Object Use objects as hash table</title> <link rel="stylesheet" href=""> </head> <body> <script type="text/javascript"> function unique(a) { var obj = {}; return (function(item) {//filter will return a member of the item that is true return (item) ? false : (obj[item] = true); }); } var a = [1, 1, 3, 2, 1, 2, 4]; var res = unique(a); (res); // [1, 3, 2, 4] </script> </body> </html>
The above is the full content of the js array operation method (must-read) that the editor brings to you. I hope everyone supports me~