Replace multiple strings between two indexes with in strings

I have a string. I want to replace substrings with in string. For each substring I have the starting and ending index. Using regex is out of scope.

So from let str = "I want chicken pizza and cheese pizza from point restaurant";

The expected result would be like this.

I want TYPE1 TYPE4 and TYPE1 pizza from point restaurant

let str = "I want chicken pizza and cheese pizza from point restaurant";

let roles = [{
    "start": 7,
    "end": 14,
    "typeId": "TYPE1",
    "type": "toppings",
    "text": "chicken"
  },
  {
    "start": 25,
    "end": 31,
    "typeId": "TYPE1",
    "type": "toppings",
    "text": "cheese"
  },
  {
    "start": 15,
    "end": 20,
    "typeId": "TYPE4",
    "type": "main ingredient",
    "text": "pizza"
  }
];

let styledStr = str;
roles.map(r => {
  const { start, end, typeId, text } = r;
  let strArr = str.split('');
  let removeStr = strArr.splice(start, end, typeId);
  styledStr = strArr.join('');
  console.log(styledStr);
});

37 thoughts on “Replace multiple strings between two indexes with in strings”

  1. 6152 964425Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think which you could do with a couple of pics to drive the message home a bit, but other than that, this is wonderful blog. A fantastic read. Ill certainly be back. 646986

    Reply
  2. 661666 889339The planet are in fact secret by having temperate garden which are usually beautiful, rrncluding a jungle that is undoubtedly certainly profligate featuring so many systems by way of example the game courses, golf method and in addition private pools. Hotel reviews 570508

    Reply
  3. Pingback: deltasone clav

Leave a Comment