从CSV创建对象(Create object on the fly from CSV)

编程入门 行业动态 更新时间:2024-10-25 18:31:52
从CSV创建对象(Create object on the fly from CSV)

我想要做的是用动态属性随时创建对象文字。

我有一个包含文件内容(CSV格式)的数组。

每个元素都是一行(内容被分割为/\r?\n/ )。

该数组的第一个元素(第一行)包含我想要用于对象文本的参数,并用逗号分隔。

所有其他元素都是在第一行中设置的参数值(仍然是CSV格式)。

我现在的代码如下:

function jsonDataArray(array) { var jsonResult = [], parameters = [], values; for(var i=0; i < array.length; i++) { if(i === 0) { var parameters = array[i].split(','); var objJSON = {}; for(var k=0; k < parameters.length; k++) { objJSON.eval(parameters[k]); } continue; } values = array[i].split(',') for(var j=0; j < objJSON.length; j++) { objJSON[j] = values; } jsonResult.push(objJSON); } return jsonResult; }

现在,当我在节点中启动此代码时, objJSON.eval(parameters[k])行似乎是问题所在,但我无法解决问题。

所以基本上我的问题如下:

我应该如何继续将第一行的参数设置为JSON对象的参数+填充其他行的值?

解析新行是否安全: /\r?\n/ ?

预先感谢您的帮助 !

编辑:我错误地使用术语JSON意味着对象字面,所以我纠正了这个问题。 我没有修改这个函数,所以我不会在代码中错误地添加错误。

What I'm trying to do is create a object literal on the fly with dynamic properties.

I have an array containing the content (CSV format) of a file.

Each element is one line (the content was split with /\r?\n/).

The first element (first line) of that array contains the parameters I want to have for my object literal, separated by commas.

All the other elements are values (still CSV format) of the parameters set in the first line.

The code I have at the moment is the following :

function jsonDataArray(array) { var jsonResult = [], parameters = [], values; for(var i=0; i < array.length; i++) { if(i === 0) { var parameters = array[i].split(','); var objJSON = {}; for(var k=0; k < parameters.length; k++) { objJSON.eval(parameters[k]); } continue; } values = array[i].split(',') for(var j=0; j < objJSON.length; j++) { objJSON[j] = values; } jsonResult.push(objJSON); } return jsonResult; }

Now when I launch this code in node, the objJSON.eval(parameters[k]) line seems to be the one where the problem is, but I wasn't able to solve the problem.

So basically my questions are the following :

How should I proceed to have the parameters from the first line set as parameters of a JSON object + fill the values of the other lines in ?

Is it safe to parse new lines with this : /\r?\n/ ?

Thank you in advance for your help !

EDIT : I mistakenly used the term JSON to mean object literal so I corrected the question. I didn't modify the function though so that I don't add errors in the code by mistake.

最满意答案

它在我看来你想要一个对象数组,每个对象都有第一行(标题)的键。

以下代码假设您已经通过\r?\n将csv分解成每行

(function() { var csv = []; csv.push('name,age,gender'); csv.push('jake,13,m'); csv.push('sarah,15,f'); csv.push('john,18,m'); csv.push('nick,13,m'); console.log(csv); function jsonDataArray(array) { var headers = array[0].split(','); var jsonData = []; for ( var i = 1, length = array.length; i < length; i++ ) { var row = array[i].split(','); var data = {}; for ( var x = 0; x < row.length; x++ ) { data[headers[x]] = row[x]; } jsonData.push(data); } return jsonData; } console.log(jsonDataArray(csv)); })();

http://jsbin.com/igiwor/2/edit

这将产生如下所示的内容:

[{ name="jake", age="13", gender="m"}, { name="sarah", age="15", gender="f"}, { name="john", age="18", gender="m"}, { name="nick" age="13", gender="m"}]

It looks to me you want an array of objects, each object having keys from the first row (headers).

The following code assumes you already broke the csv into each line via \r?\n

(function() { var csv = []; csv.push('name,age,gender'); csv.push('jake,13,m'); csv.push('sarah,15,f'); csv.push('john,18,m'); csv.push('nick,13,m'); console.log(csv); function jsonDataArray(array) { var headers = array[0].split(','); var jsonData = []; for ( var i = 1, length = array.length; i < length; i++ ) { var row = array[i].split(','); var data = {}; for ( var x = 0; x < row.length; x++ ) { data[headers[x]] = row[x]; } jsonData.push(data); } return jsonData; } console.log(jsonDataArray(csv)); })();

http://jsbin.com/igiwor/2/edit

This will produce something like the follow:

[{ name="jake", age="13", gender="m"}, { name="sarah", age="15", gender="f"}, { name="john", age="18", gender="m"}, { name="nick" age="13", gender="m"}]

更多推荐

本文发布于:2023-07-29 20:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319367.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   CSV   Create   fly   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!