如何重新排列 CSV / JSON 键列? (Javascript)
How to rearrange CSV / JSON keys columns? (Javascript)
我正在使用 Papa Parse JavaScript 库将 JSON 对象数组转换为 CSV。有没有办法让 CSV 列以某种方式排列。
例如;我得到的列为:
OrderStatus、canOp、OpDesc、ID、OrderNumber、FinishTime、UOM、StartTime
但希望安排为:
ID、OrderNumber、OrderStatus、StartTime、FinishTime、canOp、OpDesc、UOM
我之所以得到未排列的 CSV 是因为 JSON 数据如下所示:
json = [
{
OrderStatus: "Good",
canOp:"True",
OpDesc:"Good to go",
ID:"100",
OrderNumber:"1000101",
FinishTime:"20:50",
UOM:"K",
StartTime:"18:10"
},
...
]
谢谢
Papa Parse 允许在 unparse()
函数中指定字段顺序:
var csv = Papa.unparse({
fields: ["ID", "OrderNumber", "OrderStatus", "StartTime", "FinishTime", "canOp", "OpDesc", "UOM"],
data: [{
OrderStatus: "Good",
canOp: "True",
OpDesc: "Good to go",
ID: "100",
OrderNumber: "1000101",
FinishTime: "20:50",
UOM: "K",
StartTime: "18:10"
},
// ...
]
});
console.log(csv);
<script src="https://unpkg.com/papaparse@4.6.3/papaparse.min.js"></script>
<h3>See your dev console for the results</h3>
您不需要任何框架即可将 json 转换为 csv。
// fields in the order you want them
const fields = ['ID', 'OrderNumber', 'OrderStatus', 'StartTime', 'FinishTime', 'canOp', 'OpDesc', 'UOM'];
const mapper = (key, value) => value === null ? '' : value // how to handle null values
const csv = [
fields.join(','), // header row
...json.map((row) => fields.map((fieldName) => JSON.stringify(row[fieldName], mapper))).join(',')
]
console.log(csv.join('\r\n'))
输出:
ID,OrderNumber,OrderStatus,StartTime,FinishTime,canOp,OpDesc,UOM
"100","1000101","Good","18:10","20:50","True","Good to go","K"
"100","1000101","Good","18:10","20:50","True","Good to go","K"
我正在使用 Papa Parse JavaScript 库将 JSON 对象数组转换为 CSV。有没有办法让 CSV 列以某种方式排列。
例如;我得到的列为:
OrderStatus、canOp、OpDesc、ID、OrderNumber、FinishTime、UOM、StartTime
但希望安排为:
ID、OrderNumber、OrderStatus、StartTime、FinishTime、canOp、OpDesc、UOM
我之所以得到未排列的 CSV 是因为 JSON 数据如下所示:
json = [
{
OrderStatus: "Good",
canOp:"True",
OpDesc:"Good to go",
ID:"100",
OrderNumber:"1000101",
FinishTime:"20:50",
UOM:"K",
StartTime:"18:10"
},
...
]
谢谢
Papa Parse 允许在 unparse()
函数中指定字段顺序:
var csv = Papa.unparse({
fields: ["ID", "OrderNumber", "OrderStatus", "StartTime", "FinishTime", "canOp", "OpDesc", "UOM"],
data: [{
OrderStatus: "Good",
canOp: "True",
OpDesc: "Good to go",
ID: "100",
OrderNumber: "1000101",
FinishTime: "20:50",
UOM: "K",
StartTime: "18:10"
},
// ...
]
});
console.log(csv);
<script src="https://unpkg.com/papaparse@4.6.3/papaparse.min.js"></script>
<h3>See your dev console for the results</h3>
您不需要任何框架即可将 json 转换为 csv。
// fields in the order you want them
const fields = ['ID', 'OrderNumber', 'OrderStatus', 'StartTime', 'FinishTime', 'canOp', 'OpDesc', 'UOM'];
const mapper = (key, value) => value === null ? '' : value // how to handle null values
const csv = [
fields.join(','), // header row
...json.map((row) => fields.map((fieldName) => JSON.stringify(row[fieldName], mapper))).join(',')
]
console.log(csv.join('\r\n'))
输出:
ID,OrderNumber,OrderStatus,StartTime,FinishTime,canOp,OpDesc,UOM
"100","1000101","Good","18:10","20:50","True","Good to go","K"
"100","1000101","Good","18:10","20:50","True","Good to go","K"