Javascript 脚本 - 将 CSV 文件读入 SVG 脚本

问题描述 投票:0回答:1

Javascript新手,我正在尝试调整用于显示SVG图像的脚本,当前脚本读取脚本内的值表单,我想调整它以从CSV读取值,我遇到的问题是脚本似乎没有正确解释该值,我遇到问题的列是“nodeStyle”,如果我将列变量从nodeStyle更改为{width:100,height:100}它可以工作,但不会从CSV读取它,任何帮助都会很棒

//Data.CSV format
//id, x, y, href, nodeStyleW,nodeStyle,properties,label_text
//NODE22,650,50,/icon.png,{width:100,height:100},,NODE22
    fetch('/data.csv')
   .then(response => response.text())
   .then(data => {

     const rows = data.split('\n').slice(1); // Skip header row
      rows.forEach(row => {
     const [id, x, y, href, nodeStyle, properties, label_text] = row.split(/,(?![^{]*})/);


     net.addNode(id,parseInt(x),parseInt(y),href,nodeStyle,properties,label_text);

       });
      });
    
     // Mode added via script 
    net.addNode("NODE2",600,50,"/images/icon.png",{width:100,height:100},"","","NODE2");

我尝试过直接添加该值,这有效

net.addNode(id,parseInt(x),parseInt(y),href,nodeStyle,属性,label_text); net.addNode(id,parseInt(x),parseInt(y),href,{宽度:100,高度:100},属性,label_text);

javascript regex csv fetch constants
1个回答
0
投票

如果您确定

nodeStyle
始终是 JSON 格式的有效对象描述符,则 parse 该 JSON:

net.addNode(id,
            parseInt(x),
            parseInt(y),
            href,
            JSON.parse(nodeStyle),
            properties,
            label_text);
© www.soinside.com 2019 - 2024. All rights reserved.