我正在使用.map函数将元素转换为数组,然后我想将此数组转换为字符串:
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>
控制台输出:
debug: fiddle.jshell.net:29
["false", "true", "true", "false", "false", "false", "false"]
Sunday,Monday,Tuesday,Wednesday,Thursday
[object Object]
在这里小提琴:http: //jsfiddle.net/F8ufE/
如何将 selectedElements 转换为数组?