1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How can I remove text(e.g ["88664734","88639280","88676217"]) from a strReviewers string using JavaS

Discussion in 'JavaScript' started by asifakhtar, Nov 22, 2022.

  1. #1
    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"]));
     
    asifakhtar, Nov 22, 2022 IP