-1

I have a Json file

[
"Cooling":
  {
    "id": 1,
    "title": "Cooling",
    "description": "Lorem Ipsum is simply ${dummy} text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took"
  }
]

I want to replace the ${dummy} to something else base on some conditions before rendering. Is this possible in react?

I am importing this file in my react App.js component and displaying it as:

return (
    <div>
      <div className="row">
        {Json.map(item => (
          <div className="col-md-1">
            <hr />
            <p key={item.id}>
              {item.title}
            </p>
            <p>{item.description}</p>
          </div>
        ))}
      </div>
    </div>
  );

here is it possible to replace the ${dummy} in the item.description before render?

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Damini Ganesh
  • 288
  • 1
  • 10
  • 1
    Looks like a simple string replace to me, what have you tried? – Dan D Jun 03 '19 at 15:13
  • @DanD Do you mean like `something ${expression}` ? I did try that but the JSON file just said wrong syntax. I wanted something like { replacestring } where replace string is a prop – Damini Ganesh Jun 03 '19 at 15:14
  • 1
    `item.replace("${expression}", "my text")` is a starting point https://www.w3schools.com/jsref/jsref_replace.asp – Dan D Jun 03 '19 at 15:16
  • @DanD ahh so you are just treating the ${expression} as a string. Ya that ll work. Was just Assuming react will have something super different as I am new to react .. :p But sure this ll work thank you – Damini Ganesh Jun 03 '19 at 15:19
  • Possible duplicate of [How to replace all occurrences of a string in JavaScript](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – Emile Bergeron Jun 03 '19 at 15:33

1 Answers1

0

you can use string replace

return (
    <div>
      <div className="row">
        {Json.map(item => (
          <div className="col-md-1">
            <hr />
            <p key={item.id}>
              {item.title}
            </p>
            <p>
              {
                item.description.replace('${dummy}', `${your_variable}`)
              }
            </p>
          </div>
        ))}
      </div>
    </div>
  );
Joseph
  • 682
  • 5
  • 17