Adrija
Adrija

Reputation: 99

Formatting a string in Javascript using DOJO

I have a JSON file and I need to replace one Property in it using DOJO Format. This is my JSON File.

{
    "In1": "00",
    "In2": "01",
    "In3": "02",
    "In4": "04",
    "Pay": "$pay",
}

my code is :

require(["dojo/string"],function(string) {
   object.["Pay"] = string.substitute("${pay}", { replace: "ABC"});
});

I need to replace {$pay} with "ABC"

Its coming to string undefined :(.

Upvotes: 1

Views: 289

Answers (2)

Bourbia Brahim
Bourbia Brahim

Reputation: 14712

that doesn't require extras lib just use the .replace() function to change what ever you want , whether using text or regular expression

see below snippet :

object = {
  "In1": "00",
  "In2": "01",
  "In3": "02",
  "In4": "04",
  "Pay": "$pay",
}

object["Pay"] = object["Pay"].replace("$pay", "ABC")

console.log(object);

Upvotes: 1

Adrija
Adrija

Reputation: 99

Finally I figured it out :)

Here is what I used.

require(["dojo/string"],function(string) { object.["Pay"] = string.substitute("${pay}", { pay: "ABC"});});

But couldn't figure out a way, if the JSON is like

{
 "Pay": "$pay",
}

:(

Upvotes: 0

Related Questions