How can I remove text from a strReviewers string using JavaScript or jQuery?

How can I remove text(e.g [“88664734”,“88639280”,“88676217”]) from a strReviewers string which contains list of Reviewers separated by semicolon and then join the whole string again either using JavaScript or jQuery?

I get a dynamic string(strReviewers) which contains multiple user records separated by comma:

I need to remove whole user record if I pass an array of ids. e.g [“88664734”,“88639280”,“88676217”]

var strReviewers = "88664734*,*Andrew Farmer*,*19042*,**,*,19013,19017,19042,19043,19051,*;*88639280*,*Sally Hopewell*,*19042*,**,*,19013,19017,19042,19043,*;*88686221*,*Jonathan Rees*,*19042*,**,*,19013,19017,19042,19043,19060,*;*88676217*,*James Wason*,*19042*,**,*,19013,19017,19042,19043,*;*";

strReviewers contains user records separated by semicolon and each user record is separated by ,.

Each record contains 1 user which is in the shape of userid then following by name then following by roleid then following by txtSpeciality following by then rolelist.

type or paste code here
/*
88664734*,*Andrew Farmer*,*19042*,**,*,19013,19017,19042,19043,19051,*;
*88639280*,*Sally Hopewell*,*19042*,**,*,19013,19017,19042,19043,*;
*88686221*,*Jonathan Rees*,*19042*,**,*,19013,19017,19042,19043,19060,*;
*88676217*,*James Wason*,*19042*,**,*,19013,19017,19042,19043,*;
*/

I have done it using the following code but wondering this can be achieved some other easier way?
function removeReviewerByID(ids = []) {
   return strReviewers
        .split(";")
        .map(item => item.split("*,*"))      
        .filter(item => item[0] !== "*")
        .map(item => ({
          userid:item[0],
          name:item[1],
          roleid:item[2],
          txtSpeciality:item[3],
          rolelist:item[4]
        }))        
        .filter(item => (!ids.includes(item["userid"]) && !ids.includes(item["userid"].replace(/\*/g, ''))))
        .map(item => ({
          record: item["userid"].concat("*,*").concat(item["name"]).concat("*,*").concat(item["roleid"]).concat("*,*").concat(item["txtSpeciality"]).concat("*,*").concat(item["rolelist"]).concat(";")
        }))
        .reduce((accumulator, item) => {  
          return accumulator.concat(item["record"]);
        }, "")        
}

console.log(removeReviewerByID(["88664734","88639280","88676217"]));

You could do it like this:

function removeReviewerByID(ids) {
  return strReviewers
           .split(";")
           .filter((el) => {
             const records = el.split(',');
             const id = records[0].replace(/\*/g, "");

             return (!id || ids.includes(id))? false : true;
           })
           .join(';')
           .concat(';');
}

This produces:

console.log(removeReviewerByID(["88664734","88639280","88676217"]));

// *88686221*,*Jonathan Rees*,*19042*,**,*,19013,19017,19042,19043,19060,*;

Does that work for you, or have I overlooked something?

Txs, very smart, really appreciated. This is exactly what I am looking for.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.