Search code examples
node.jsreactjsreplaceuser-input

How to replace String with user input values for storing in a variable?


Here is the code :

var raw = "{\r\n   \"__metadata\": {\r\n        \"type\": \"SP.Data.TestPostListItem\"\r\n    },\r\n    \"Title\": \"Ankit\",\r\n    \"EmpID\": \"698\"\r\n}";

I have two user input values which I want to replace. Ankit with name and 698 with empid

I have stored the user input values using useState like this

const [name, setName] = useState("");
const [empid, setEmpid] = useState("");

Can anyone help me with this please?


Solution

  • You just have to convert your raw string into an object:

    const obj = JSON.parse(raw);
    
    obj.Title = name;
    obj.EmpId = empid;