0

I’ve created a string…

{"atts": [{"name": "wedw"}, {"type": "---"}]}

I pile a bunch of these together in an array based on user input and attach them to another string to complete a JSON object that tests out as valid.

So I end up with a global array called fields with a bunch of these little snippets.

So how do I change the name "weds" with a new name? I’ve tried...

function changefieldname(pos){
var obj = JSON.parse(jsonstring);
var oldname = obj.tracelog.fields[pos].atts[0]["name"];
var newname = document.getElementById("newlogfieldname"+pos).value;
fields[pos].replace(oldname, newname);
//writejson();
}

And a bunch of variations. I know everything is checking out correct interms of the variables pos, oldname, and newname. I also know that fields[pos] returns the string in the array I want to correct but it’s not happy. I also tried converting fields[pos] to a string, but the replace function doesn't work on it. I’m sure there is a good reason.

3
  • Shouldn't it simply be obj.tracelog.fields[pos].atts[0].name = newname? Commented May 27, 2014 at 3:22
  • stackoverflow.com/questions/4430193/… Commented May 27, 2014 at 3:24
  • Not the way it is written. The fields are defined globally and continuously replace a portion of the tracelog string. But your observation makes me wonder if that makes any sense, and that the fields should be defined within the function. It would make your suggestion work, and probably be cleaner. thanks' Commented May 27, 2014 at 3:32

1 Answer 1

1

Why even bother trying to do some kind of replacement? Just assign it a new value:

obj.tracelog.fields[pos].atts[0]["name"] = newname;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.