2

So i have a list of locations and this is being stored it the following:

var locations;

When i console.log this i get the following:

CA,CH,LA

How could i do this so i get a list of the locations and without the commas. I was thinking a foreach loop but this didnt work.

I the achieved outcome would look this this

CA
CH
LA
0

3 Answers 3

5

You can call join() on an array to create a string from each element concatenated together by any character you need.

// without commas
console.log(locations.join('')); // = "CACHLA"

// by newline (\n)
console.log(locations.join("\n")); // =
// CA
// CH
// LA
Sign up to request clarification or add additional context in comments.

2 Comments

your awesome :D Iv never heard of join before, thanks
$(".postcode_check").each(function(){ if($(this).val() == pre_locations.join("\n")){ $(this).prop('checked', true); } });
1

locations is an Array so, explicitly call Array.join on it, providing it a new-line character \n

console.log(locations.join("\n"));

Comments

1

use join function to remove comma

<script>
var locations=['CA','CH','LA'];
alert(locations.join("\n"));   
</script>

Output enter image description here

Comments

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.