-1

I have the following code, guess what the string doesn't get replaced for some reason, if I try manually agreements.replace('{customername}', 'some string'); everything works fine.

  var keywords = [
    {
      key: '{customername}',
      desc: 'Customer Name',
      map: 'user_name'
    },

    {
      key: '{vesselname}',
      desc: 'Customer Vessel Name',
      map: 'vessel_name'
    }
  ];

  var parseData = {
    user_name: "Some name",
    vessel_name: "Some Vessel",
    spot_title: "My Spot",
    today: new Date().toDateString()
  };

  var agreements = "{customername}, some customer, {vesslname} -> here";

  for(var i = 0; i < keywords.length; i++) {
    console.log(keywords[i]['key'], parseData[keywords[i]['map']]);

    agreements.replace(
        keywords[i]['key'],
        parseData[keywords[i]['map']]
    );
  }

  alert(agreements);

Please ignore I'm adding some more detail, well stackoverflow makes it a requirement, interesting...

8
  • .replace returns new string and does not replaces original string. Just assign it back Commented Oct 29, 2016 at 11:38
  • @Rajesh Oh come on, huh. =) Commented Oct 29, 2016 at 11:39
  • @Rajesh thanks, that just worked. Commented Oct 29, 2016 at 11:41
  • Its alright. Also, I have requested to delete the answer. Once done, please delete the question as it will not add much to portal Commented Oct 29, 2016 at 11:46
  • Sure. Is js inconsistent? at least it seems like that to me, sorting an array does change the actual array, such as: arr = [3, 2, 1]; arr.sort(); console.log(arr); outputs: [1, 2, 3], but str = "a b c"; str.replace('a', 'z'); doesn't change the original variable. Commented Oct 29, 2016 at 14:03

1 Answer 1

0

change code to this:

  agreements = agreements.replace(
        keywords[i]['key'],
        parseData[keywords[i]['map']]
    );

Sign up to request clarification or add additional context in comments.

1 Comment

Just a pointer, you should not answer question with obvious mistake. Rather search post that has already answered it and share its link. Soon, you will get rights to vote to close. Once you have them, just mark it as duplicate. Also if its not much trouble, please delete the answer. I know its not wrong, but there is no need for this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.