How to check duplicates in two columns with JavaScript and by their values?
<p>I have an array of data like this:</p>

<pre><code>var data = [
    // Columns
    ['Target ID', 'Source ID', 'Label'],

    // Key: 1
    [1, 2, 'String 1/2'],
    [1, 3, 'String 1/3'],
    [1, 4, 'String 1/4'],
    [1, 5, 'String 1/5'],

    // Key: 2
    [2, 1, 'String 2/1'],
    [2, 3, 'String 2/3'],
    [2, 4, 'String 2/4'],
    [2, 5, 'String 2/5'],

    // Key: 3
    [3, 1, 'String 3/1'],
    [3, 2, 'String 3/2'],
    [3, 4, 'String 3/4'],
    [3, 5, 'String 3/5']
]
</code></pre>

<p>As you can see, I have duplicate items in the array above, ie:</p>

<pre><code>[1, 2, 'String 1/2']
</code></pre>

<p>And:</p>

<pre><code>[2, 1, 'String 2/1']
</code></pre>

<p>I can't figure out the logic to remove those kind of duplicates.</p>

<p>All what I have achived so far is to remove duplicated items by first key value (since the array/data is created from a CSV), ie. <code>[1, 1, 'String 1/2']</code>.</p>

<p>Basically my question is: how to remove duplicated items from the array above?</p>

<hr>

<p><strong>EDIT:</strong> Better explanation of my problem.</p>

<p>The string at the index <code>2</code> in the array has nothing to do with anything - it's just a descriptor.</p>

<p>First item in the array is the key - in the examples I provided they are - <code>1, 2, 3</code> - thus, a second item is related to the key elsewhere in the array, thus, a relation between <code>1, 2</code> and <code>2, 1</code> is a duplicate - and I want those removed.</p>

<p><strong>EDIT 2:</strong> Added the column item in the array above:</p>

<p>So, now I hope it's more clear -- first item in each array is the <code>Target ID</code>, second is the <code>Source ID</code> and the third one is not relavant at all.</p>

<p>My goal and need is to remove all items which already occur in the array, if the <code>Target ID === Source ID</code> in any of the previous arrays/items.</p>

<p>I have tried something dumb as:</p>

<pre><code>var in_target = [];
var in_source = [];

var reformed_data = [];

for(var x in data) {
    var _target = data[x][0];
    var _source = data[x][1];

    if(in_target.indexOf( _target ) === -1) {
        if(in_source.indexOf( _source) === -1) {
            reformed_data.push( data );
        }
    }

    in_target.push( _target );
    in_source.push( _source );
}

console.log( reformed_data );
</code></pre>

<p>But is doesn't work and it's terribly slow, since I actually have a large piece of data.</p>