0

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.

Amit Kumar
  • 13
  • 1
  • 5
  • 1
    Why do you need to do this? The reason you see `null` in the console is because that's the value of the property. You see `''` when using `$.trim()` as the property is coerced to a string. There is absolutely no point looping through to make all `null` values in to `''` as the coercion will happen automatically when you use the property. – Rory McCrossan Apr 07 '16 at 10:07
  • Iterate properties and update them with trimmed content; – itzmukeshy7 Apr 07 '16 at 10:07
  • No need for jQuery. Just add a default value like so: console.log( data.value1 || ""); – Yogi Apr 07 '16 at 10:20
  • jQuery.trim() or $.trim() http://stackoverflow.com/questions/4040259/trim-to-remove-white-space – vijay rathore Apr 07 '16 at 10:23

1 Answers1

0

Try:

Object.keys(data).forEach(function(key) {
   data[key] = $.trim(data[key]);
});
jcubic
  • 61,973
  • 54
  • 229
  • 402