0

I am attempting to print out several parts of an array as a string. However when I do print out the string, there are spaces before the commas which is giving me some trouble.

Here is some example code:

var Arr = [];
var foo = "Hello";
var voo = "World";

if (foo != null) {
  Arr.push(foo);
}

if (voo != null) {
  Arr.push(voo);
}

console.log(Arr.join(', '));

How can one print out said string without the spaces before the commas?
EDIT: I should clarify that in my actual code, I am pushing values from a tab-delimited text file into an array.

  • Possible duplicate of [How to remove spaces from a string using JavaScript?](https://stackoverflow.com/questions/5963182/how-to-remove-spaces-from-a-string-using-javascript) – Dacre Denny Feb 04 '19 at 23:47
  • 1
    Also, you shouldn't call a variable `Array`, that's a reserved keyword. – Ibu Feb 04 '19 at 23:49
  • 1
    @ibu— Array is not a [*reserved word*](http://ecma-international.org/ecma-262/9.0/#sec-reserved-words), it's the name of a built–in function/constructor. But yes, bad choice of variable name. – RobG Feb 04 '19 at 23:50
  • 1
    This may be an error only in the question but you have a typo here: `(voo !- null)`.the comparison symbol should be `!=` – Ibu Feb 04 '19 at 23:50
  • I see `voo !- null`, a colon after `Array.push(voo)`, typos everywhere. This isn't valid syntax, and your code works as is, and should not output any space. – Blue Feb 04 '19 at 23:51
  • Also fix the colon you have after Array.push(voo): to ; – sjdm Feb 04 '19 at 23:52
  • 2
    @Ryan Call `trim()` on the string that you're adding to the array, to trim off spaces before they're added to the array. – Blue Feb 04 '19 at 23:56

5 Answers5

0

Your code should print the correct output. (Fixing the obvious typos that were in your original post). You likely have an issue with your original code (A space after what is stored in the variable foo).

var arr = [];  
var foo = "Hello";  
var voo = "World";  

if (foo != null){  
  arr.push(foo);  
}  
if (voo != null){  
  arr.push(voo);
}  

console.log(arr.join(', '));  
Blue
  • 22,608
  • 7
  • 62
  • 92
0

Fixing all the minor errors in your code produces:

var myArray = [];
var foo = "Hello";
var voo = "World";

if (foo != null) {
  myArray.push(foo);
}

if (voo != null) {
  myArray.push(voo);
}

console.log(myArray.join(', '));

A brief summary:

  • Array is a built-in type - don’t use it as a variable name.
  • !- is not a valid operator, did you mean !=?
  • : at the end of a line is invalid syntax, did you mean ;?
  • console.println() doesn’t exist, did you mean console.log()?
MTCoster
  • 5,868
  • 3
  • 28
  • 49
0

Basically, when you joined the array, you were specifying a space after the comma, so why not remove it like below:

Arr.join(',');
Jovan
  • 64
  • 5
0

So, I am assuming the spaces you mentioned are the tab spaces based on the recent question edit.

You can use regex to replace the '\t' with ' ' single space or no space as required.

foo.replace(/\t/g, "");

And if you just want to concat two strings, I wonder why you cannot just use the '+' operator with the comma rather than creating an auxiliary array to store them?

Dhananjai Pai
  • 5,914
  • 1
  • 10
  • 25
0
  1. you can remove commas like this and print without space console.log(Arr.join(''));

  2. if you want keep space console.log(Arr.join(' '));

3.want tab space console.log(Arr.join('\t'));

ishan weerakoon
  • 419
  • 1
  • 5
  • 6