denmodo
denmodo

Reputation: 41

Remove duplicate lines with position delimiter

I need a function javascript. I have a list with format

abc|a|1
abc|a|2
abcd|a|1
abc|b|1

And i want to duplicate by first and second postion (split "|") New list is:

abc|a|1
abcd|a|1
abc|b|1

Thank you so much

Upvotes: 2

Views: 39

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386654

You could filter the data with Array#filter() and take a temporary object for lookup if there is already an item with the same two parts in the result.

var list = ['abc|a|1', 'abc|a|2', 'abcd|a|1', 'abc|b|1'],
    result = list.filter(function (a) {
        var b = a.split('|'), 
            k = b[0] + '|' + b[1];
        if (!this[k]) {
            this[k] = true;
            return true;
        }
    }, {});

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Upvotes: 2

Related Questions