0

I have 4 different values, when looping through an array. Is it possible to add the values together like you would in Java's StringBuilder append?

I want something like this when doing a console.log():

28.0334307,-25.872523799999996, 28.031527552564445,-25.87632233243363

Now I am just getting it one for one like this when doing a console.log():

28.0334307
-25.872523799999996
28.031527552564445
-25.87632233243363

Here is my code:

var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]

for(var item in coordinates)
{
   console.log(item);
}

4 Answers4

1

Try this:

var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]

console.log(coordinates.join(' '));

var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]

console.log(coordinates.join(' '));
Aadil Mehraj
  • 2,435
  • 5
  • 16
1

you can get a string in-line separated by a comma with join() method, try this:

var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]
console.log(coordinates.join(', '));
sonEtLumiere
  • 3,650
  • 3
  • 7
  • 26
1

The browser consoles displays an array like that in to be more readable. It's not an actual structural representation of how an array is. What you want is basically a string created by joining the elements of the array. Array.join method can be used for this:

coordinates.join("'")
Ankit
  • 132
  • 1
  • 7
1

Use JavaScript array join() method to display values separated by comma. The join() method returns the array as a string. The elements will be separated by a specified separator. The default separator is a comma (,).

tripleee
  • 158,107
  • 27
  • 234
  • 292
sdk
  • 11
  • 3