0

I have a very odd error. Here is the output of a array if i do a console.log(array);

[" TRUE ", " FALSE "]

I want the output to be

["TRUE", "FALSE"]

There should not be any whitespaces between the quotes. I hope you get my point. so the array elements doesnt have spaces actually.

Could anyone pls let me know how to remove the whitespace. If there are whitespaces between the double quotes, i am not able to display the details to checkbox in jqgrid.

2

3 Answers 3

5
var arr = [" TRUE ", " FALSE "]
for(var i=0; i< arr.length; i++) {
    arr[i] = arr[i].trim();
}
Sign up to request clarification or add additional context in comments.

Comments

3

an alternative to @jrath's answer:

var arr = [" TRUE ", " FALSE "];
arr = arr.map(function(value){
  return value.trim()
});

BTW you should almost certainly fix this problem where you make the array, not where you display the array

1 Comment

@Plato- i get your point. I had the array prepared at the backend using perl. the process of creating an array was complicated. hence thought of formating it at the browser side before displaying.
0
function format(arr){
 $.each(arr, function(index,item){ arr[index] = arr[index].trim()});
}

3 Comments

$ represents the jquery short form. $.each or jquery.each, can iterate on a list or array and you can call a function with each element of the array
$ is alias for jqery. btw @jraths answer worked fine.
Reason I was asking is that I don't see the jQuery tag in question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.