2D arrays comparison - nested for loop error
<p>I have the following function which compares the first column value in two 2D arrays.</p>

<pre><code>var new_array = [['EMI_007','lion','cat'],
                 ['EMI_008','cat','dog']
                ];

var existing_array = [['EMI_002','cat','tiger'],
                     ['EMI_004','hen','pig'],
                     ['EMI_007','pigeon','hen'],
                     ['EMI_001','boar','tiger']
                ];
</code></pre>

<p>The value in new_array[i][0] is compared to the value in existing_array[j][0].</p>

<p>If they are the same, the entire row is replaced by the row in <code>new_array</code>, otherwise the  <code>new_array</code> row is concatenated to the existing array.</p>

<p>In the above example, new_array is compared to existing_array.</p>

<p>EMI_007 exists in 'existing_array', so it's replaced by <code>['EMI_007','lion','cat']</code> and ['EMI_008','cat','dog'] is appended to the existing array.</p>

<p>I have created the following function but there seems to be an error.</p>

<p>If a row with an EMI id found in new_array does not exist in 'existing_array', it is inserted several times.</p>

<pre><code>function concat_2D_array(existing_array, new_array){
    for (var i=0; i&lt;new_array.length; i++) {
        for (var j=0; j&lt;existing_array.length; j++) {
            if(new_array[i][0] == existing_array[j][0]){
            alert("ok,it already exists in the existing_array");
            existing_array.splice(j,1,new_array[i]);
            }
            else{
            alert("It is not in the existing array");
            // existing_array.concat(new_array[i]);
            }
        }
    }
    alert("returned existing_array"+existing_array)
    return existing_array;
}
</code></pre>

<p>Any help is most appreciated</p>
