Reputation: 164
Below, I have json from which I want to remove underscores. Here is what I tried.
I am getting getting "replace is not a function".
var y= {
"QUESTION_MARKS": {
"count": 390,
"percentage": 8.8
},
"NEARLY_ACTIVATED": {
"count": 710,
"percentage": 16
},
"MOST_VALUBLE_SUBSCRIBERS": {
"count": 1650,
"percentage": 37.2
},
"LIKELY_TO_THRIVE": {
"count": 300,
"percentage": 6.8
},
"EMAIL_ENGAGE": {
"count": 420,
"percentage": 9.5
},
"EMAIL_INACTIVE": {
"count": 32,
"percentage": 0.7
},
"NEVER_ACTIVATED": {
"count": 930,
"percentage": 21
}
};
var yformatted=y.replace(/[_-]/g, " ");
easeSummary=yformatted;
Upvotes: 0
Views: 1386
Reputation: 17
Wrap your object in string templates because is a multi-line object and then could apply to replace()
.
var y= `{
"QUESTION_MARKS": {
"count": 390,
"percentage": 8.8
},
"NEARLY_ACTIVATED": {
"count": 710,
"percentage": 16
},
"MOST_VALUBLE_SUBSCRIBERS": {
"count": 1650,
"percentage": 37.2
},
"LIKELY_TO_THRIVE": {
"count": 300,
"percentage": 6.8
},
"EMAIL_ENGAGE": {
"count": 420,
"percentage": 9.5
},
"EMAIL_INACTIVE": {
"count": 32,
"percentage": 0.7
},
"NEVER_ACTIVATED": {
"count": 930,
"percentage": 21
}
}`;
var yFormatted= y.replace(/_/g,' ');
The solved problem is here: MissyM stackblitz
Upvotes: 0
Reputation: 63550
Using this SO answer as a template you could write a function to return a new object with new keys:
var y = {"QUESTION_MARKS":{"count":390,"percentage":8.8},"NEARLY_ACTIVATED":{"count":710,"percentage":16},"MOST_VALUBLE_SUBSCRIBERS":{"count":1650,"percentage":37.2},"LIKELY_TO_THRIVE":{"count":300,"percentage":6.8},"EMAIL_ENGAGE":{"count":420,"percentage":9.5},"EMAIL_INACTIVE":{"count":32,"percentage":0.7},"NEVER_ACTIVATED":{"count":930,"percentage":21}};
function removeUSFromKey(obj) {
const keys = Object.keys(obj).map(key => {
const newKey = key.replace(/_/g, '');
return { [newKey]: obj[key] };
});
return Object.assign({}, ...keys);
}
console.log(removeUSFromKey(y))
Upvotes: 0
Reputation: 144699
replace
is a a method of string objects. y
in your code is a plain object. Plain objects do not have replace
method. You can JSON stringify the object and then use the replace
method. This may have unwanted side effects as it seems you only want to modify the keys. I'd suggest creating a new object:
const x = Object.keys(y).reduce((ret, key) => {
let nkey = key.replace(/[_-]/g, " ");
ret[nkey] = y[key];
return ret;
}, {});
Upvotes: 1
Reputation: 479
These are a few ways to tackle this problem. Here is an other Stackoverflow link addressing this problem: JavaScript: Object Rename Key
Upvotes: 0
Reputation: 369
See this fiddle for an example of using for(in)
: http://jsfiddle.net/craftman32/djb1m7p0/
var y = {
"QUESTION_MARKS": {
"count": 390,
"percentage": 8.8
},
"NEARLY_ACTIVATED": {
"count": 710,
"percentage": 16
},
"MOST_VALUBLE_SUBSCRIBERS": {
"count": 1650,
"percentage": 37.2
},
"LIKELY_TO_THRIVE": {
"count": 300,
"percentage": 6.8
},
"EMAIL_ENGAGE": {
"count": 420,
"percentage": 9.5
},
"EMAIL_INACTIVE": {
"count": 32,
"percentage": 0.7
},
"NEVER_ACTIVATED": {
"count": 930,
"percentage": 21
}
};
var newObject = {};
for (key in y) {
newObject[key.replace(/[_-]/g, " ")] = y[key];
}
console.log(newObject);
In this example, newObject
is created that has the same properties as y
but with _
removed in each property name.
Upvotes: 1
Reputation: 2646
For replace to work you need to convert your object to string and then replace _. Once that it is done convert it back to object. Take a look at below snippet.
var y = {
"QUESTION_MARKS": {
"count": 390,
"percentage": 8.8
},
"NEARLY_ACTIVATED": {
"count": 710,
"percentage": 16
},
"MOST_VALUBLE_SUBSCRIBERS": {
"count": 1650,
"percentage": 37.2
},
"LIKELY_TO_THRIVE": {
"count": 300,
"percentage": 6.8
},
"EMAIL_ENGAGE": {
"count": 420,
"percentage": 9.5
},
"EMAIL_INACTIVE": {
"count": 32,
"percentage": 0.7
},
"NEVER_ACTIVATED": {
"count": 930,
"percentage": 21
}
};
var yformatted = JSON.stringify(y).replace(/[_-]/g, " ");
var easeSummary = JSON.parse(yformatted);
console.log(easeSummary);
Upvotes: 1
Reputation: 1455
You must convert your JSON object to a String, then you can use replace function to do what you are looking for
var yformatted=JSON.stringify(y).replace(/[_-]/g, " ");
Upvotes: 2