0

I have an array & I am trying to replace a substring of a particular key's value w/ another substring.

Example key:value

var myobj = {};    
key1: "FieldName[8] = \"field9\";"

I was trying to change the [8] to [9]

var kstr = '[' + k + ']'; // [8]
var dbstr = '[' + dbindex + ']'; // [9]
myobj[key1].replace(kstr, dbstr);
console.log('Key1: ' + myobj[key1]) // Still has [8] in it !
1
  • does the value of myobj[key1] is "FieldName[8] = \"field9\";"? Commented Jul 25, 2018 at 2:00

1 Answer 1

3

As the MDN documentation for String.prototype.replace() states:

Note: The original string will remain unchanged.

So, you need to assign the result of the replace() operation back to the variable:

myobj[key1] = myobj[key1].replace(kstr, dbstr);
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.