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.
/*
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?
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,*;*";
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"]));
3
Answers
One approach would be to use a regular expression. Turn the array of IDs into a pattern by joining by
|
, and then filter for records that don’t start with the pattern.To keep the data structure consistent, consider using
.match(/.*?*;*/g)
instead of.split(";")
– by matching, you’ll be preserving the formatting (where the first item doesn’t start with a*
, and where the final item is terminated by,*;*
)It would be possible to do it in a single regular expression replacement, though it’d be more confusing.
/(?:^|(?<=*;*))(?:88664734|88639280|88676217)*.*?*;*/g
means:(?:^|(?<=*;*))
– Match either^
– Start of line, or(?<=*;*)
– A position preceded by*;*
(the start of a reviewer section)(?:88664734|88639280|88676217)
– Match any of the IDs*
– Match a literal*
(make sure the ID ends here).*?
– Match any characters, lazily, until encountering*;*
– The characters*;*
This approach matches and removes the delimiter between reviewers at the end of each match.
Another approach
Here is a short and sweet answer using a
.split()
,.filter()
, and.join()
:Output:
Explanation:
excludedRegex
is the regex representation of theexcluded
array