Reputation: 215
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 !
Upvotes: 0
Views: 42
Reputation: 97331
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);
Upvotes: 3