1

I have created a script that removes part of a string if any of these characters are present:, & and / I have created a function that returns the string with the portion of text I need. But I want to join all conditionals in the function, since it is not good technique to have 3 IFs.

Example:

currentArtist = 'Dimitri Vegas & Like Mike,Paris Hilton/Dimitri Vegas'

Return: Dimitri Vegas

Example2:

currentArtist = 'Tiësto, Jonas Blue , Rita Ora'

Return: Tiësto

this.refreshLyric = function(currentArtist) {
    if (currentArtist.includes("&")) {
        let i = currentArtist.indexOf("&");
        var currentArtist = cleanText(currentArtist, i);
    } 
        if (currentArtist.includes("\/")) {
            let i = currentArtist.indexOf("\/");
            var currentArtist = cleanText(currentArtist, i);
        } 
            if (currentArtist.includes(",")) {  
                let i = currentArtist.indexOf(",");
                var currentArtist = cleanText(currentArtist, i);
            } 
}       

function cleanText(artist, index){
 let currentArtist = artist.substring(0,index);
 return currentArtist.trim();
}
Always Helping
  • 14,316
  • 4
  • 13
  • 29
Juan
  • 195
  • 9
  • 2
    Does this answer your question? [How do I split a string with multiple separators in javascript?](https://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript) – Justinas Aug 18 '20 at 06:39
  • I'm going to study it well and try it. Thanks for the advice. – Juan Aug 18 '20 at 06:41

2 Answers2

2

Maybe using a regex is an easier way to do this:

const regex = /[&\/,].*$/;
let currentArtist;

currentArtist = 'Dimitri Vegas & Like Mike,Paris Hilton/Dimitri Vegas';
currentArtist = currentArtist.replace(regex, '');
console.log(currentArtist);


currentArtist = 'Tiësto, Jonas Blue , Rita Ora';
currentArtist = currentArtist.replace(regex, '');
console.log(currentArtist);
Hao Wu
  • 17,573
  • 6
  • 28
  • 60
  • One more question, @Justinas has suggested another thread to answer my question, I have already chosen your answer as correct, what should I do with that query, I answer no? I mean this: "A community member has suggested a similar question that may solve your problem." – Juan Aug 18 '20 at 06:55
  • @Juan Ideally you do not want this type of solution since this will give you wanted BUT its `replacing` the whole text you have inside. What if want to take out `Mike Paris` - its manipulation the original text you have there – Always Helping Aug 18 '20 at 06:58
  • @AlwaysHelping you are absolutely right, with the other thread I will learn to do it in all cases. It is that I do not want to harm the user who helped me here. I'm new and I don't know what will happen if I choose the other. You understand me? – Juan Aug 18 '20 at 07:01
  • 1
    @Juan That's absolutely fine, pick whichever the one helps you the most. You don't even have to accept any answer if you feel none is helpful. BTW, if you don't want to replace the original string, you can just assign it to a different variable: `let somethingElse = currentArtist.replace(regex, '');` – Hao Wu Aug 18 '20 at 07:02
  • 1
    @Juan No there is no harm in choosing what you like. See my answer below as well. Its not replacing anything its only giving you what you need and can give other names as well. if you want – Always Helping Aug 18 '20 at 07:04
  • 1
    Man, yours has helped me, it has solved the problem for me that is why I have chosen it. I only asked what happened if I accepted the other, my intention is NOT to harm you under any circumstances. I'm a noob. – Juan Aug 18 '20 at 07:04
1

You can simply use String#split() function to get your desired output! Its simple and you are replacing anything from your original string or words you have.

split() return an array of words that matched the condition.

Live Demo:

let currentArtist = 'Dimitri Vegas & Like Mike,Paris Hilton/Dimitri Vegas'.split('&')[0].trim();
let currentArtist2 = 'Tiësto, Jonas Blue , Rita Ora'.split(',')[0].trim();
console.log(currentArtist) //Dimitri Vegas 
console.log(currentArtist2) //Tiësto
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
Always Helping
  • 14,316
  • 4
  • 13
  • 29