简体   繁体   中英

How to use trim function on an object to remove whitespaces jquery

I have an object 'data' and it has some elements, some of them have null value. When i am printing them instead of whitespace null is printed.

Example:

console.log(data.value1);
console.log(data.value2);
console.log(data.value3);

Output is :

null
null
null

But when using

$.trim(data.value1);
$.trim(data.value2);
$.trim(data.value3);

Problem is solved.

Now how can i use trim on object so i dont have to write same code for all the elements.

Try:

Object.keys(data).forEach(function(key) {
   data[key] = $.trim(data[key]);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM