Rob Rob - 1 month ago 19
Javascript Question

JQuery remove duplicate from array where string contains same text

I have an array with X number of items. Each has variables separated by a pipe character. In a loop I can split on the pipe to get the second item; but how do I splice to remove the duplicate.

"Sometext|22621086|address|333629dc87894a7ea7df5291fa6d1836|PC_E|1803"
"Sometext2|22622138|working|d3e70175ffe942568cd21f1cf96f4d63|PC_E|1803"
"Sometext3|22622138|working|851946e6325445da99c113951590f714|PC_E|1803"


Results should be this.

"Sometext|22621086|address|333629dc87894a7ea7df5291fa6d1836|PC_E|1803"
"Sometext2|22622138|working|d3e70175ffe942568cd21f1cf96f4d63|PC_E|1803"


Note that the duplicate 22622138 is a random number so the solution needs to work for any number in this location (it's always in the arr[1] position).

This is what I tried:

$.each(arr_transcript, function (i, e) {
if (e.length != 0) {
var arr = e.split("|")
var i = arr_transcript.indexOf(arr[1]);
if (i != -1) {
arr_transcript.splice(i, 1);
}
}
});

Answer

Here's a generic function:

function uniqBy(a, key) {
    let seen = new Set();
    return a.filter(item => {
        let k = key(item);
        return !seen.has(k) && seen.add(k);
    });
};

var data = [
    "Sometext|22621086|address|333629dc87894a7ea7df5291fa6d1836|PC_E|1803",
    "Sometext2|22622138|working|d3e70175ffe942568cd21f1cf96f4d63|PC_E|1803",
    "Sometext3|22622138|working|851946e6325445da99c113951590f714|PC_E|1803"
];

var result = uniqBy(data, item => item.split('|')[1]);

console.log(result)

See here for more info.