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);
                }
            }
        });

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
@DelightedD0D: those are arrow functions
Thanks, wish I could upvote again just for that, very interesting!!
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)

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.