admin管理员组文章数量:1025465
I have been trying to use trim()
for json key value pair , but it seems not to work, can anyone please help ?
var user = { first_name: "CSS",
last_name: " H ",
age: 41,
website: "java2s"
};
for (var key in user) {
console.log((key+"-->"+user[key].trim());
}
I have been trying to use trim()
for json key value pair , but it seems not to work, can anyone please help ?
var user = { first_name: "CSS",
last_name: " H ",
age: 41,
website: "java2s."
};
for (var key in user) {
console.log((key+"-->"+user[key].trim());
}
Share
Improve this question
edited Jun 21, 2018 at 15:13
Eazy
16813 bronze badges
asked Jun 21, 2018 at 14:51
NinjaDevNinjaDev
3011 gold badge8 silver badges19 bronze badges
3 Answers
Reset to default 3Assign the trimmed value to the specific user[key]
:
var user = { first_name: "CSS", last_name: " H ", age: 41, website: "java2s." };
for (var key in user) {
user[key] = user[key].toString().trim()
console.log(key+"-->"+user[key]);
}
Also, if you cannot use .trim()
because of older browsers, use a polyfill for that.
Remended:
function trim(string) {
return string.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
Evil edition:
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
All browsers since IE9+ have trim()
For those browsers who does not support trim(), you can use this polyfill from MDN:
if (!String.prototype.trim) {
(function() {
// Make sure we trim BOM and NBSP
var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
String.prototype.trim = function() {
return this.replace(rtrim, '');
};
})();
}
That's because you can't access a JSON
value like:
var value = json[key]; // Wrong
You must do it like this:
var value = json.key; // Correct
I have been trying to use trim()
for json key value pair , but it seems not to work, can anyone please help ?
var user = { first_name: "CSS",
last_name: " H ",
age: 41,
website: "java2s"
};
for (var key in user) {
console.log((key+"-->"+user[key].trim());
}
I have been trying to use trim()
for json key value pair , but it seems not to work, can anyone please help ?
var user = { first_name: "CSS",
last_name: " H ",
age: 41,
website: "java2s."
};
for (var key in user) {
console.log((key+"-->"+user[key].trim());
}
Share
Improve this question
edited Jun 21, 2018 at 15:13
Eazy
16813 bronze badges
asked Jun 21, 2018 at 14:51
NinjaDevNinjaDev
3011 gold badge8 silver badges19 bronze badges
3 Answers
Reset to default 3Assign the trimmed value to the specific user[key]
:
var user = { first_name: "CSS", last_name: " H ", age: 41, website: "java2s." };
for (var key in user) {
user[key] = user[key].toString().trim()
console.log(key+"-->"+user[key]);
}
Also, if you cannot use .trim()
because of older browsers, use a polyfill for that.
Remended:
function trim(string) {
return string.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
Evil edition:
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
All browsers since IE9+ have trim()
For those browsers who does not support trim(), you can use this polyfill from MDN:
if (!String.prototype.trim) {
(function() {
// Make sure we trim BOM and NBSP
var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
String.prototype.trim = function() {
return this.replace(rtrim, '');
};
})();
}
That's because you can't access a JSON
value like:
var value = json[key]; // Wrong
You must do it like this:
var value = json.key; // Correct
本文标签: javascriptremove whitespace inside json object key value pairStack Overflow
版权声明:本文标题:javascript - remove whitespace inside json object key value pair - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745628488a2160012.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论