Skip to content

Instantly share code, notes, and snippets.

@HereChen
Created July 26, 2017 02:51
Show Gist options
  • Save HereChen/800172ead1b5fd20650ae4323f8bee45 to your computer and use it in GitHub Desktop.
Save HereChen/800172ead1b5fd20650ae4323f8bee45 to your computer and use it in GitHub Desktop.
删除对象中key对应值的空格和换行符
// 剔除前后空格和换行符
function trimWrapSpace(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
// 如果是字符串, 直接去除末尾空格以及换行符.
// 注意变量中的换行符和实际换行
obj[key] = (typeof obj[key] === 'string' || obj[key] instanceof String) ? obj[key].replace(/[\\r\\n]+/g, '').replace(/^\s+|\s+$/g, '') : obj[key];
}
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment