Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    39
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Compare 2 arrays

    I have 2 arrays and I would like to compare the 2 arrays.
    If an element in array 1 is NOT in array 2 then I would like to display that element.

    In this case, I should only display the letter "c" but it doesn't work and I don't know why??
    Here's my code:


    Code:
    <html><head>
    <script type="text/javascript">
    function getValue(id){
       var x=new Array("a","b","c","d","e");
       var y=new Array("a","b","3","d","e");
       var str="";
    
       for (var i=0; i<x.length; i++){
          for (var j=0; j<y.length; j++){
             if (x[i] == y[j]){
           	    break;
    	 }else{
                //Check if reach the last element in the array 2
                //If yes, then display that element in array 1 b/c not in array 2
                if (y[j] == y.length-1){
    	       str += x[i];
                }
             }
          }
       }
       document.getElementById(id).innerHTML = str;
    
    }
    
    
    function init(){
       getValue("info");
    }
    </script>
    </head>
    
    <body onload="init()">
    <h2 id="info"></h2>
    </body>
    </html>

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,331
    Thanks
    13
    Thanked 348 Times in 344 Posts
    I wouldn't use a nested loop ...
    PHP Code:
    for (var x.lengthl--;) {
      if (-
    === y.indexOf(x[l])) {
        
    str += x[l];
      }

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,799
    Thanks
    78
    Thanked 4,411 Times in 4,376 Posts
    This makes no sense:
    Code:
    if (y[j] == y.length-1){
    That would be doing, for example,
    Code:
    if (y[4] == 4){
    And in any case it is unneeded.

    Code:
    function getValue(id)
    {
        var x=new Array("a","b","c","d","e");
        var y=new Array("a","b","3","d","e");
        var notFounds = [];
    
        for (var xi=0; xi < x.length; xi++)
        {
            var xtest = x[xi];
            for (var yi=0; yi<y.length; yi++)
            {
                if ( xtest == y[yi])
                {
           	        xtest = null;
                    break;
    	    }
             }
             if ( xtest != null ) notFounds.push(xtest);
       }
       document.getElementById(id).innerHTML = notFounds.join(",");
    
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    hiyatran (12-16-2011)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,799
    Thanks
    78
    Thanked 4,411 Times in 4,376 Posts
    Older MSIE doesn't have Array.indexOf, so if you need this to work universally, do use a nested loop.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,331
    Thanks
    13
    Thanked 348 Times in 344 Posts
    then I'd rather add an indexOf() method to the Array prototype than making it more complicated. older versions of IE are such a nuissance.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,799
    Thanks
    78
    Thanked 4,411 Times in 4,376 Posts
    It depends on what this is for. If it's something that is done once per page, and for not many array elements, it's actually going to be more work to create the indexOf on the Array prototype than it's worth. If it's done on many pages and/or involves large arrays, then yeah, I'd add to the prototype. The performance on older IE will suck, but it would anyway.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Dec 2011
    Location
    Earth if you want to know
    Posts
    27
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by hiyatran View Post
    I have 2 arrays and I would like to compare the 2 arrays.
    If an element in array 1 is NOT in array 2 then I would like to display that element.

    In this case, I should only display the letter "c" but it doesn't work and I don't know why??
    Here's my code:


    Code:
    <html><head>
    <script type="text/javascript">
    function getValue(id){
       var x=new Array("a","b","c","d","e");
       var y=new Array("a","b","3","d","e");
       var str="";
    
       for (var i=0; i<x.length; i++){
          for (var j=0; j<y.length; j++){
             if (x[i] == y[j]){
           	    break;
    	 }else{
                //Check if reach the last element in the array 2
                //If yes, then display that element in array 1 b/c not in array 2
                if (y[j] == y.length-1){
    	       str += x[i];
                }
             }
          }
       }
       document.getElementById(id).innerHTML = str;
    
    }
    
    
    function init(){
       getValue("info");
    }
    </script>
    </head>
    
    <body onload="init()">
    <h2 id="info"></h2>
    </body>
    </html>
    Hi there,

    Please take a look at this:

    Code:
    function isInArray($e, $a) {
        for (var $j in $a) {
            if ($a[$j] == $e) {
                return true;
            };
        };
        return false;
    };
    
    function dif($a, $b) {
        var $aNotB = [];
        var $bNotA = [];
        for (var $k in $a) {
            if (!isInArray($a[$k], $b)) {
                $aNotB[$aNotB.length] = $a[$k];
            };
        };
        for (var $k in $b) {
            if (!isInArray($b[$k], $a)) {
                $bNotA[$bNotA.length] = $b[$k];
            };
        };
        return [$aNotB, $bNotA];
    };
    The function dif(Array1, Array2) will return an array. The first element of that array will be an array which contains values that appear in the Array1 but not in Array2. The second element of that array will be an array which contains values that appear in the Array2 but not Array1.

    Here is the test:

    Code:
    var a = [1, 3, 4, 8];
    var b = [2, 3, 8, 9];
    var result = dif(a, b);
    document.write("Element(s) that is in the first Array but not in the second Array: " + result[0]);
    document.write("<br>Element(s) that is in the second Array but not in the first Array: " + result[1]);
    Result:
    Code:
    Element(s) that is in the first Array but not in the second Array: 1,4
    Element(s) that is in the second Array but not in the first Array: 2,9
    I hope this help.

    Tim_


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •