SoFunction
Updated on 2025-02-28

Summary of js array operation methods (must read)

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

&lt;!DOCTYPE html&gt; 
&lt;html lang="en"&gt; 
  &lt;head&gt; 
    &lt;meta charset="utf-8"&gt; 
    &lt;title&gt;Array deduplication01&lt;/title&gt; 
  &lt;/head&gt; 
  &lt;body&gt; 
  &lt;script type="text/javascript"&gt; 
  //Add method to array prototype     = function(){ 
      var arr = []; 
      for(var i=0,i=;i&lt;len;i++){ 
        if((this[i]) == -1){ 
          (this[i]); 
        } 
      } 
      return arr; 
    }; 
    ([1,2,3,2,5,6,3].unique());//[1, 2, 3, 5, 6] 
  &lt;/script&gt; 
  &lt;/body&gt; 
&lt;/html&gt; 

Method 2

&lt;!DOCTYPE html&gt; 
&lt;html lang="en"&gt; 
  &lt;head&gt; 
    &lt;meta charset="utf-8"&gt; 
    &lt;title&gt;Array deduplication02&lt;/title&gt; 
  &lt;/head&gt; 
  &lt;body&gt; 
  &lt;script type="text/javascript"&gt; 
     = function(){ 
    var n = {}, 
        r=[]; //n is a hash table, r is a temporary array    for(var i = 0; i &lt; ; 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] 
  &lt;/script&gt; 
  &lt;/body&gt; 
&lt;/html&gt; 

Method 3

&lt;!DOCTYPE html&gt; 
&lt;html lang="en"&gt; 
  &lt;head&gt; 
    &lt;meta charset="utf-8"&gt; 
    &lt;title&gt;Array deduplication&lt;/title&gt; 
  &lt;/head&gt; 
  &lt;body&gt; 
  &lt;script type="text/javascript"&gt; 
     = function(){ 
      var arr = [this[0]];//Result array      for(var i=1;i&lt;;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] 
  &lt;/script&gt; 
  &lt;/body&gt; 
&lt;/html&gt; 

Method 4

&lt;!DOCTYPE html&gt; 
&lt;html&gt; 
&lt;head&gt; 
  &lt;meta charset="utf-8"&gt; 
  &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt; 
  &lt;title&gt;filter&lt;/title&gt; 
  &lt;link rel="stylesheet" href=""&gt; 
&lt;/head&gt; 
&lt;body&gt; 
&lt;script type="text/javascript"&gt; 
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] 
&lt;/script&gt; 
&lt;/body&gt; 
&lt;/html&gt; 

Method 5

&lt;!DOCTYPE html&gt; 
&lt;html&gt; 
&lt;head&gt; 
  &lt;meta charset="utf-8"&gt; 
  &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt; 
  &lt;title&gt;Object Use objects as hash table&lt;/title&gt; 
  &lt;link rel="stylesheet" href=""&gt; 
&lt;/head&gt; 
&lt;body&gt; 
&lt;script type="text/javascript"&gt; 
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] 
&lt;/script&gt; 
&lt;/body&gt; 
&lt;/html&gt; 

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~