Convert Javascript Array To String


Answer :

If value is associative 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. :)



You can use .toString() to join an array with a comma.



var array = ['a', 'b', 'c'];
array.toString(); // result: a,b,c


Or, set the separator with array.join('; '); // result: a; b; c.



Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable