0

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);
                }
            }
        });
georg
  • 211,518
  • 52
  • 313
  • 390
Rob
  • 1,226
  • 3
  • 23
  • 41

2 Answers2

3

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.

Community
  • 1
  • 1
georg
  • 211,518
  • 52
  • 313
  • 390
  • What exactly is `item => item.split('|')[1]` and `a.filter(item =>` doing here? Specifically what is the role of `=>` I know it's use in php but have never seen it used in javascript before – Wesley Smith Oct 12 '16 at 20:47
  • 1
    @DelightedD0D: those are [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) – georg Oct 12 '16 at 20:54
  • Thanks, wish I could upvote again just for that, very interesting!! – Wesley Smith Oct 12 '16 at 20:57
2

Create a map of the numbers you want to check against, and then filter based on that

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

var map = arr_transcript.map(function(text) { 
    return text.split('|')[1];
});

var filtered = arr_transcript.filter(function(item, index) {
    return index === map.lastIndexOf( map[index] );
});

console.log(filtered)
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • the georg solution works; however if you obfuscate your script through an online tool, they break. I ultimately switched to adeneo's solution and the obfuscators work! – Rob Oct 29 '16 at 14:43