Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

clear whitespace from object javascript

var obj = {
  "  employee   name": "Sumit Vishwakarma",
  "employee id ": 001,
  "email ": "abc@gmail.com",
  "mobile  no": 9999999999,
};

Object.keys(obj).forEach((key) => {
   var replacedKey = key.trim().toUpperCase().replace(/ss+/g, "_");
   if (key !== replacedKey) {
      obj[replacedKey] = obj[key];
      delete obj[key];
   }
});
console.log(obj);

result
  EMAIL: "abc@gmail.com",
  EMPLOYEE ID: 1,
  EMPLOYEE_NAME: "Sumit Vishwakarma",
  MOBILE_NO: 9999999999
}
 
PREVIOUS NEXT
Tagged: #clear #whitespace #object #javascript
ADD COMMENT
Topic
Name
3+6 =