Javascript 使用.each从表中创建字符串,如何在每行之后添加换行符?

Javascript 使用.each从表中创建字符串,如何在每行之后添加换行符?,javascript,jquery,arrays,Javascript,Jquery,Arrays,我使用以下javascript从表的选定单元格中获取数据: var cellIndexMapping = { 0: true, 1: true, 3:true, 4:true, 5:true}; var data = []; $j("#myTable tr").each(function(rowIndex) { $j(this).find("td").each(function(cellIndex) { if (cellIndexMapping[cellIndex])

我使用以下javascript从表的选定单元格中获取数据:

var cellIndexMapping = { 0: true, 1: true, 3:true, 4:true, 5:true};
var data = [];

$j("#myTable tr").each(function(rowIndex) {
    $j(this).find("td").each(function(cellIndex) {
        if (cellIndexMapping[cellIndex])
            data.push($j(this).text()  );
    });
});

var fullCSV = data.join(", ");
console.log (fullCSV);
这将在逗号分隔的数组中提供所有表元素。例如,如果我的桌子是

<th>| zero | one | two | three | four | five | </th>
---------------------------------------------
<tr>|  A   |  B  |  C  |  D    |  E   |  F   | </tr>
---------------------------------------------
<tr>|  G   |  H  |  I  |  J    |  K   |  L   | </tr>
我需要的是在每行之间有一个换行符
“\n”
。因此,我期望的结果如下所示:

A,B,D,E,F,\n G,H,J,K,L \n
有什么想法吗

var cellIndexMapping = { 0: true, 1: true, 3:true, 4:true, 5:true},
    data = [],
    finalData = [];

$j("#myTable tr").each(function(rowIndex) {
    data.push([]);

    $j(this).find("td").each(function(cellIndex) {
        if (cellIndexMapping[cellIndex])
            data[rowIndex].push( $j(this).text() );
    });
});

$j.each(data, function(i, e) {
    finalData.push( e.join(',') );
});

finalData.join("\n");

或者,您可以在每个循环中添加
\n

var cellIndexMapping = { 0: true, 1: true, 3:true, 4:true, 5:true},
    finalData = '';

$j("#myTable tr").each(function(rowIndex) {
    var data = [];
    $j(this).find("td").each(function(cellIndex) {
        if (cellIndexMapping[cellIndex])
            data.push( $j(this).text() );
    });

    finalData += data.join(', ') + "\n";
});
看这把小提琴:


或者,您可以在每个循环中添加
\n

var cellIndexMapping = { 0: true, 1: true, 3:true, 4:true, 5:true},
    finalData = '';

$j("#myTable tr").each(function(rowIndex) {
    var data = [];
    $j(this).find("td").each(function(cellIndex) {
        if (cellIndexMapping[cellIndex])
            data.push( $j(this).text() );
    });

    finalData += data.join(', ') + "\n";
});

查看此提琴:

您只需将其添加到外循环的末尾即可:

var res = "";
$j("#myTable tr").each(function(rowIndex) {
    var data = [];
    $j(this).find("td").each(function(cellIndex) {
        if (cellIndexMapping[cellIndex])
            data.push($j(this).text()  );
    });
    res += data.join(", ") + "\n";
});

现在,
res
保存最终值。

您只需将其添加到外部循环的末尾:

var res = "";
$j("#myTable tr").each(function(rowIndex) {
    var data = [];
    $j(this).find("td").each(function(cellIndex) {
        if (cellIndexMapping[cellIndex])
            data.push($j(this).text()  );
    });
    res += data.join(", ") + "\n";
});

现在,
res
保存最终值。

您确定要在第一行中使用尾随逗号吗?难道你不想要像这样的东西吗

A,B,D,E,F\nG,H,J,K,L\n
以下是一种使用方法:


确实要在第一行中使用尾随逗号吗?难道你不想要像这样的东西吗

A,B,D,E,F\nG,H,J,K,L\n
以下是一种使用方法:


谢谢,这很有效,但是现在我在每行的开头都有一个逗号。谢谢你的帮助。我仍然无法让它与此一起正确返回。Joesph的解决方案对我很有效,所以我将继续使用它。@Zac-这很酷。对于未来的网友,这里有一把小提琴展示了这种方法的有效性:谢谢,这很有效,但现在我在每一行的开头都有一个逗号。谢谢你的帮助。我仍然无法让它与此一起正确返回。Joesph的解决方案对我很有效,所以我将继续使用它。@Zac-这很酷。对于未来的网民来说,这里有一把小提琴展示了这种方法的有效性:是的,这是一个打字错误。不过,我刚刚返回了[object HTMLTableCellElement]。我正在使用约瑟夫的解决方案。谢谢您的帮助。忘记将内部的
每个
更改为
映射
。它现在起作用了:是的,那是个打字错误。不过,我刚刚返回了[object HTMLTableCellElement]。我正在使用约瑟夫的解决方案。谢谢您的帮助。忘记将内部的
每个
更改为
映射
。它现在可以工作了: