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? 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"]));
A basic js way of doing it, then you can map it the way you want [using map]. <script> 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 = strReviewers.replace(/\*/g,'').split(';'); //split records for (k in strReviewers) ///you can use js .forEachor jQuery .each { if(strReviewers[k].trim() == '') delete strReviewers[k]; else strReviewers[k] = strReviewers[k].split(',').filter(function (el) { return el.trim() != ''; }); } console.log(strReviewers); </script> Code (markup): Output [ [ "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" ] ] Code (markup):
I dont think your solution works because strReviewers = strReviewers.replace(/\*/g,'').split(';'); //split records This returns array not an object and for...in statement iterates over all enumerable string properties of an object.