I am using the .map function to convert elements to an array, and I’d like to then convert this array into a string:
javascript:
var selectedElements = $('.data').map(function() {
return $(this).hasClass('selected') ? 'true' : 'false';
});
var A = ['Sunday','Monday','Tuesday','Wednesday','Thursday']
A = A + "";
var string = selectedElements + "";
console.log("\ndebug:");
console.log(selectedElements);
console.log(A);
console.log(string );
html:
<div class='data'></div>
<div class='data selected'></div>
<div class='data selected'></div>
<div class='data'></div>
<div class='data'></div>
<div class='data'></div>
<div class='data'></div>
Console output:
debug: fiddle.jshell.net:29
["false", "true", "true", "false", "false", "false", "false"]
Sunday,Monday,Tuesday,Wednesday,Thursday
[object Object]
Fiddle here: http://jsfiddle.net/F8ufE/
How do I convert the selectedElements to array ?
According to the
.map()docs:So if you want to get the actual array you can use
getmethod, like this:For converting an array to a string you can use
joinmethod: