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;
2
  • 4
    y is not a string. It's an object. Commented Sep 24, 2018 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 Commented Sep 24, 2018 at 15:49

7 Answers 7

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, " "); 
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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.

Comments

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;
}, {});

Comments

0

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

Comments

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))

Comments

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

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.