gpt4 book ai didi

javascript - 将对象数组更改为 CSV 模式

转载 作者:行者123 更新时间:2023-12-03 11:38:54 25 4
gpt4 key购买 nike

我有以下数组:

var objRow = [
{
2011-09-20 : [0, 100, 0],
customerID : C1101,
ANI : 1234
},
{
2011-09-25 : [0, 0, 0],
customerID : C1101,
ANI : 1234
},
{
2011-09-20 : [0, 500, 0],
customerID : C1102,
ANI : 5678
},
{
2011-09-22 : [0, 0, 50],
customerID : C1102,
ANI : 5678
}
]

我想从上面的数组创建 CSV 数据。但是,我在将该数组更改为 CSV 模式时遇到问题:

1234, C1101, 0, 0, 100, 0, 0, 0
5678, C1102, 0, 0, 500, 0, 0, 50

我尝试使用reducecustomerID进行分组,因为每个对象中的第一个索引是日期。我有一些日期数组:

var dateArr = ["2011-09-20", "2011-09-22", "2011-09-25"];

这是我的代码:

var result = objRow.reduce(function(prev, curr, index, arr) {
var num = curr["customerID"];

if (!prev[num]) {
prev[num] = [];
}

for (var j = 0; j < dateArr.length; j++) {
prev[num].push(curr[dateArr[j]]);
}

return prev;
}, {});

更新问题用于date索引中的数字组合。我使用这个规则:

[0, 100, 0] // from first Object
[0, 0, 0] // from second Object


fistObject_firstIndex, secondObject_firstIndex, firstObject_secondIndex, secondObject_secondIndex, firstObject_thirdIndex, secondObject_thirdIndex
0, 0, 100, 0, 0, 0

上,下,上,下,上,下......

如何创建上面的 CSV 模式?谢谢...

最佳答案

我认为这会给你你想要的结果:

var objRow = [{
date: 2011-09-20,
nums: [0, 100, 0],
customerID: "C1101",
ANI: 1234
}, {
date: 2011-09-25,
nums: [0, 0, 0],
customerID: "C1101",
ANI: 1234
}, {
date: 2011-09-20,
nums: [0, 500, 0],
customerID: "C1102",
ANI: 5678
}, {
date: 2011-09-22,
nums: [0, 0, 50],
customerID: "C1102",
ANI: 5678
}];


//CREATE CSV-FORMATTED STRINGS
var csvLine = "";
var numsArray = new Array();
for (var i=0; i<objRow.length; i++) {
//check if this is the first element with a new 'ANI' (which means a new CSV line starts)
if (objRow[i-1]==(undefined||null) || objRow[i].ANI!=objRow[i-1].ANI) {
//if so, start a new string
csvLine = objRow[i].ANI +", "+ objRow[i].customerID +", "; //add the 'ANI' and 'customerID'
numsArray.length = 0; //clear array
numsArray.push(objRow[i].nums); //store the 'nums' in a separate array
} else {
//if not, add to the existing string
numsArray.push(objRow[i].nums); //store the 'nums' in a separate array
}

//check if this is the last element with the same 'ANI' (which means this CSV line is complete)
if (objRow[i+1]==(undefined||null) || objRow[i].ANI!=objRow[i+1].ANI) {
//add the 'nums' of every object in intertwining order (every 1st, every 2nd, etc.)
for (var k=0; k<numsArray[0].length; k++) {
for (var j=0; j<numsArray.length; j++) {
csvLine += numsArray[j][k].toString() +", ";
}
}
//remove the last comma
if (csvLine.substring(csvLine.length-2) == ", ") {
csvLine = csvLine.substring(0,csvLine.length-2);
}
//output the CSV line
document.getElementById("csv").innerHTML += csvLine + "<br />";
}
}
<div id="csv"></div>

( fiddle :http://jsfiddle.net/5gyp3ce6/16/ )

我必须稍微更改一下您的数组,因为要使其工作,数组键必须全部相同。
另外,我必须将 ID 更改为字符串,否则无法定义它们。

而不是将其写入 <div>最后,您当然可以将该行添加到另一个变量中,将其写入文件或其他内容。

如果代码中的注释不够清楚,请留下评论,我会尽力解释得更好。

关于javascript - 将对象数组更改为 CSV 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26351853/

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