4
I’m trying to join the contents of an array of strings into one sentence, but I can only bring the whole content separated by commas. Detail, this array comes from a JSON file that is consumed by another JSON file.
The output of my code is:
["John","Peter","Sally","Jane"]
What I need:
John Peter Sally Jane
<!DOCTYPE html>
<html>
<body>
<h2>Create JSON string from a JavaScript array.</h2>
<p id="demo"></p>
<script>
var arr = [ "John", "Peter", "Sally", "Jane" ];
var myJSON = JSON.stringify(arr);
document.getElementById("demo").innerHTML = myJSON;
</script>
</body>
</html>
In that case, I used the method JSON.stringify
, but someone knows another method that will enable me to do what I need?
Note: I don’t use any programming language, it’s a JSON file that consumes data from another, so it’s not possible to simply loop by concatenating the array strings. I used HTML here only to display code output.
Use the method
join()
to join all elements of an array ...var arr = [ "John", "Peter", "Sally", "Jane" ]; document.getElementById("demo").innerHTML = arr.join(' ');
– NoobSaibot