Reputation: 683
I'm trying to replace all instances of a character in an object properties values
I'm stuck here. I can't figure out how to modify the value.
for(let [key, val] in obj){
if(typeof val === "string"){
???? = val.replace(/,/g, '')
}
}
Upvotes: 0
Views: 62
Reputation: 206478
Using Object.entries() and Array.prototype.forEach()
const obj = { a1: "aaaa,11,aa", b2: "bbbb,22,bb"};
Object.entries(obj).forEach(([key, val]) => obj[key] = val.replace(/,/g, ""));
console.log(obj)
Upvotes: 1
Reputation: 2880
You can use Object.keys
Object.keys(obj).forEach((key) => (obj[key] = obj[key].replace("A", "n")));
Upvotes: 1
Reputation: 782025
You can't use destructuring to iterate over an object's properties and values with for-in
.
And in order to replace the value, you have to use an object accessor, you can't replace with destructuring.
for (let key in obj) {
if (typeof obj[key] == "string") {
obj[key] = obj[key].replace(/,/g, '');
}
}
Upvotes: 2