目標:將多個 JavaScript 陣列轉換為 CSV 列。
背景關系:使用 API 通過 GPS 單元掃描一組資產,并包含不活動資產的串列,以及其他指標。
問題:我需要幫助的部分是嵌套 for 回圈中的演算法,該演算法確定將單元格插入正確行/列的索引。
我在 NPM 上查看了許多軟體包,但似乎無法輕松將多個陣列轉換為 CSV 列。
我還不需要在這里提問,所以我當然可能以錯誤的方式解決這個問題或錯過了解決方案;因此,我們非常感謝實作目標的任何幫助或提示。
運行代碼如下,但所需的輸出是:
All Assets,Idle Assets
1000,2001,
1001,2002,
1002,2003,
1003,2004,
1004,,
而不是:
All Assets,Idle Assets
1000,2001,
2002,
2003,
2004,
,
1001,1002,1003,1004,
//Sample Array Data (can handle more arrays/columns)
const allAssets = ['1000', '1001', '1002', '1003', '1004'];
const idleAssets = ['2001', '2002', '2003', '2004'];
////Arrays to CSV columns
const headers = ['All Assets', 'Idle Assets'];
const columns = [allAssets, idleAssets];
//Identify columnLen (# of arrays) & rowLen (length of longest array)
let longestArr = 0;
columns.forEach((arr) =>
arr.length > longestArr
? longestArr = arr.length
: undefined);
const rowLen = longestArr;
const columnLen = columns.length;
const heading = (headers.join(',')).concat('\n');
const csvConstructor = () => {
const data = [];
for (let column = 0; column < columnLen; column ) {
const columnArray = columns[column];
for (let row = 0; row < rowLen; row ) {
let cell;
//Account for different sized arrays
columnArray[row] === undefined
? cell = ','
: cell = columnArray[row].concat(',');
//New line for cells in last column
(columns.length - 1) === column
? cell = cell.concat('\n')
: undefined;
//Dynamically insert cell into appropriate data index
const insertAt = column row; //<--- Need help here
data.splice(insertAt, 0, cell);
}
}
const result = heading.concat(data.join(''));
return result
}
const csv = csvConstructor();
console.log(csv);
uj5u.com熱心網友回復:
您csvConstructor()正在單獨迭代列,但對于表,您需要并行迭代列。此外,它試圖同時處理兩個不同的問題(轉換資料和構建 csv 字串),因此有點難以理解。
這個問題在邏輯上分為兩個階段。在第一階段,您需要一個格式良好的陣列來反映 CSV 的表格結構(或者如果它是一個大型資料集,那么您可能需要一個逐行生成行的迭代器,但我不會在這里做那個版本) .
輸出應采用以下格式:
const result = [
[header A, header B],
[value A, value B],
[value A, value B],
// ...etc...
]
一旦我們有了這個結構,我們就可以將它轉換成 CSV。
因此,要根據您的資料獲取該資訊:
function toCsvRows(headers, columns) {
const output = [headers]
const numRows = columns.map(col => col.length)
.reduce((a, b) => Math.max(a, b))
for (let row = 0; row < numRows; row ) {
output.push(columns.map(c => c[row] || ''))
}
return output
}
function toCsvString(data) {
let output = ''
data.forEach(row => output = row.join(',') '\n')
return output
}
function csvConstructor(headers, columns) {
return toCsvString(toCsvRows(headers, columns))
}
這是一個作業小提琴:
https://jsfiddle.net/foxbunny/fdxp3bL5/
編輯:實際上,讓我也做記憶體效率高的版本:
function *toCsvRows(headers, columns) {
yield headers
const numRows = columns.map(col => col.length)
.reduce((a, b) => Math.max(a, b))
for (let row = 0; row < numRows; row ) {
yield columns.map(c => c[row] || '')
}
}
function toCsvString(data) {
let output = ''
for (let row of data) {
output = row.join(',') '\n'
}
return output
}
function csvConstructor(headers, columns) {
return toCsvString(toCsvRows(headers, columns))
}
uj5u.com熱心網友回復:
如果您想要一種使用帶逗號的zip 兩個陣列的簡單方法。然后加入header并壓縮行\n。
const allAssets = ['1000', '1001', '1002', '1003', '1004'],
idleAssets = ['2001', '2002', '2003', '2004'],
headers = ['All Assets', 'Idle Assets'],
zip = (a, b) => a.map((k, i) => `${k}, ${b[i]||''}`),
zipLonger = (a, b) => a.length > b.length ? zip(a, b) : zip(b, a);
console.log(headers.join(', ') '\n'
zipLonger(allAssets, idleAssets).join('\n'))
結果:
All Assets, Idle Assets
1000, 2001
1001, 2002
1002, 2003
1003, 2004
1004,
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/335582.html
標籤:javascript 数组 文件 数学 拼接
