0

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;
Brian MacKay
  • 31,133
  • 17
  • 86
  • 125
saurabh kumar
  • 155
  • 5
  • 26
  • 4
    `y` is not a string. It's an object. – Ram Sep 24 '18 at 15:48
  • you can try JSON.stringify to convert it to a string, remove underscores, then JSON.parse to parse it back to an object. Not sure if it will work – Chris Li Sep 24 '18 at 15:49

7 Answers7

2

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, " "); 
Brank Victoria
  • 1,447
  • 10
  • 17
1

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);
Shubham Gupta
  • 2,596
  • 1
  • 8
  • 19
1

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.

Michael Hurley
  • 369
  • 2
  • 5
  • 15
1

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;
}, {});
Ram
  • 143,282
  • 16
  • 168
  • 197
0

These are a few ways to tackle this problem. Here is an other Stackoverflow link addressing this problem: JavaScript: Object Rename Key

Wolfyr
  • 409
  • 3
  • 11
0

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))
Andy
  • 61,948
  • 13
  • 68
  • 95
0

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

Mile
  • 17
  • 6