If value is not a plain array, such code will work fine:
var value = { "aaa": "111", "bbb": "222", "ccc": "333" };
var blkstr = [];
$.each(value, function(idx2,val2) {
var str = idx2 + ":" + val2;
blkstr.push(str);
});
console.log(blkstr.join(", "));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
(output will appear in the dev console)
As Felix mentioned, each() is just iterating the array, nothing more.
jquery - Convert javascript array to string - Stack Overflow
How do I convert an array to a string with commas in JavaScript
Is there a way to convert an array into string like ["P" ,"A","N","D","A"] to PANDA ?
Convert array to string javascript by removing commas - Stack Overflow
Videos
If value is not a plain array, such code will work fine:
var value = { "aaa": "111", "bbb": "222", "ccc": "333" };
var blkstr = [];
$.each(value, function(idx2,val2) {
var str = idx2 + ":" + val2;
blkstr.push(str);
});
console.log(blkstr.join(", "));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
(output will appear in the dev console)
As Felix mentioned, each() is just iterating the array, nothing more.
Converting From Array to String is So Easy !
var A = ['Sunday','Monday','Tuesday','Wednesday','Thursday']
array = A + ""
That's it Now A is a string. :)
In JavaScript, you can convert an array to a string with commas using the join()
method.
The join() method returns a string that concatenates all the elements of an array, separated by the specified separator, which in this case is a comma.
Here is an example:
const array = ['apple', 'banana', 'orange'];
const string = array.join(', ');
console.log(string);
// output: "apple, banana, orange"In this example, we first define an array of three fruits. Then we use the join()
method with a comma and a space as the separator to create a string that lists all the fruits with a comma and a space between each one.
You can replace the comma and space separator with any other separator you like, such as a hyphen, a semicolon, or a newline character.
It's important to note that the join() method only works on arrays, and it will throw an error if you try to use it on any other type of object.
Click here to learn more ways to Convert Array to String with Commas in JS
I have tried join and concatenate but they seem to have different results.
You can simple use the Array.prototype.join function
const A = [ '6', '6', '.', '5' ];
console.log(A.join(''));
As explain on the Documentation page of Array.prototype.join
The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
It can take as parameter the separator which will be use to separate element from the array when they are joined.
let list = ["a", "b", "c", "d"];
list.join("-"); // will return "a-b-c-d"
list.join("/"); // will return "a/b/c/d"
list.join(""); // will return "abcd"
By default the separator is ,. This means if you don't specify which character will be use as the separator it will use the , character
list.join(); // will return a,b,c,d
console.log(['6', '6', '.', '5'].join(''));