gpt4 book ai didi

javascript - 如何使用 Javascript 将 csv 格式化为 html 表格行和列

转载 作者:搜寻专家 更新时间:2023-10-31 21:59:35 24 4
gpt4 key购买 nike

如何使用 javascript 获取以下 csv 数据 block 并将其转换为 tr 和 td?

Jess,Female,04/26/1990,North Central College,Aix,Spring 2012,WebApp,
MC,Female,04/27/1991,Carnegie Mellon University,Aix,Spring 2012,WebApp,
Sharon,Female,04/03/1967,Hobart and William Smith Colleges,Aix,Spring 2012,WebApp,
Nancy,Female,08/15/1989,The New School,Aix,Spring 2011,WebApp,
Jacqueline,Female,03/18/1991,University of South Carolina,Aix,Spring 2011,WebApp,
Sydney,Female,12/11/1990,University of Vermont,Aix,Spring 2011,WebApp,
Kelsey,Female,12/08/1989,University of Vermont,Aix,Spring 2011,WebApp,
Oktavia,Female,11/05/1990,SUNY - Albany,Aix,Spring 2011,WebApp,
Courtney,Female,12/02/1988,Ithaca College,Aix,Spring 2009,WebApp,
Nike,Female,24.2.1989,Appleby College,Aix,Spring 2008,WebApp,
Linda,Female,8/26/1964,Kalamazoo College,Aix,Spring 2009,WebApp,
Allison,Female,12/15/1976,University of San Diego,Aix,Spring 2009,WebApp,
Carmen,Female,02/07/1988,Carnegie Mellon University,Aix,Spring 2008,WebApp,
Nora,Female,10/23/88,Eastern Washington University,Aix,Spring 2009,WebApp,
Jennifer,Female,10/27/79,University of Kansas,Aix,Spring 2009,WebApp,

所需的表格格式,用于 csv 数据中的每一行。

<tr><td>Jess</td> <td>Female<td><td>04/26/1990</td><td>North Central College</td><td>Aix</td><td>Spring 2012</td><td>WebApp</td></tr>

最佳答案

假设您在变量中有 CSV 数据(无论是通过 Ajax 还是其他方式检索),那么您可以使用 .split() method获取一组行并用逗号分隔每一行:

var data = // your data
var lines = data.split("\n"),
output = [],
i;
for (i = 0; i < lines.length; i++)
output.push("<tr><td>"
+ lines[i].slice(0,-1).split(",").join("</td><td>")
+ "</td></tr>");
output = "<table>" + output.join("") + "</table>";

(字符串.slice()是为了忽略每行尾随的逗号。)

演示:http://jsfiddle.net/frvQ2/

关于javascript - 如何使用 Javascript 将 csv 格式化为 html 表格行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14167677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com