-1

i have the following code in my html page and as you can see i am trying to replace the occurrence of "state" with "item" and "number" with "count" in the string s

here is an example of the response text : [{"number":177,"state":"ABONDONNE"},{"number":132,"state":"ENCOURS"},{"number":6,"state":"GAGNE"},{"number":195,"state":"PERDU"},{"number":2,"state":"REPORTE"}]

here is my javascript code:

const xhttp = new XMLHttpRequest();
            
            xhttp.onload = function() {
                var s = xhttp.responseText;
                s.replaceAll("state","item");
                s.replaceAll("number","count");

                var jsonArray = JSON.parse(s);
                console.log(s);
            }
            
            xhttp.open("GET","http://localhost:8080/plot");
            xhttp.send();

my problem is that after this code i get the same response text in the console:

[{"number":177,"state":"ABONDONNE"},{"number":132,"state":"ENCOURS"},{"number":6,"state":"GAGNE"},{"number":195,"state":"PERDU"},{"number":2,"state":"REPORTE"}]

what i am missing?

  • 2
    replaceAll returns a new string ... with replaced text ... So you need to assign it to the original variable like below: ``` s = s.replaceAll ("", ""); ``` – Siddharth Seth Jul 10 '21 at 17:24

2 Answers2

1

You don't assign functions's result anywhere. Change to:

s = s.replaceAll("state","item");
s = s.replaceAll("number","count");
szatkus
  • 1,292
  • 6
  • 14
1

You need to assign a new string returned by replaceAll function.

let updatedString = s.replaceAll("state","item")
Luka
  • 828
  • 9
  • 15