我在下面有一個 CSV 檔案,我想從中構建一個 javascript 物件。
我試圖從“類別”列中提取元素并創建一個層次結構物件,然后將資訊放在物件最低級別的“顏色”列下。
目標:

以下是我迄今為止嘗試從 CSV 中提取資訊的內容,以及如何確定如何從這里開始。什么是最好和最有效的?
let csv = `id, category, year, color, price \n
01, SUV > AWD > Sport > Benz, 2017, blue, $20 \n
02, SUV > FWD > Family > Benz, 2018, black, $20 \n
03, Sedan > AWD > BNW, 2017, white, $30 \n
04, SUV > AWD > Sport > BNW, 2012, red, $30 \n
05, Seden > RWD > Toyota, 2019, white, $30`
function extractCSV(csv){
let object = {}
let data = csv.split('\n')
for(let i = 1; i < data.length; i ){
let subData = data[i].split(',')
for(let j = 1; j < subData.length; j ){
let header = subData[j].split(">")
if(header.length > 1){
for(let k = 0; k < header.length; k ){
}
}
}
}
return object
}
提前致謝
uj5u.com熱心網友回復:
首先,兩個答案中的“更好”。
您會看到它遵循與您開始時大致相同的流程,但是,我選擇將所有決議放在前面,放入一些中間物件中。這允許后面的代碼專注于構建我們的樹。
let csv = `id, category, year, color, price \n
01, SUV > AWD > Sport > Benz, 2017, blue, $20 \n
02, SUV > FWD > Family > Benz, 2018, black, $20 \n
03, Sedan > AWD > BNW, 2017, white, $30 \n
04, SUV > AWD > Sport > BNW, 2012, red, $30 \n
05, Sedan > RWD > Toyota, 2019, white, $30`;
function parseCsv(csv) {
let object = {};
// Parse/Sanitize all the data up front
const rows = csv
.split("\n")
.filter((row, index) => index && row)
.map((row) => {
const columns = row.split(",");
return {
headers: columns[1].split(">").map((p) => p.trim()),
color: columns[3].trim()
};
});
// Then spin through the rows
for (const row of rows) {
let currentObj = object;
// Then spin through the headers
for (let hi = 0; hi < row.headers.length; hi) {
const header = row.headers[hi];
// If it is the last header, assign the color to it
if (hi === row.headers.length - 1) {
currentObj[header] = row.color;
break;
}
// Else we need to get or set another object level
currentObj = currentObj[header] = currentObj[header] ?? {};
}
}
return object;
}
console.log(parseCsv(csv));
其次,我在這里采取了另一種方法。我在遞回解決方案中找到了一些優雅。雖然,它可能更難閱讀,很可能會使用更多的記憶體,并且速度更慢,尤其是檔案/標題深度增加時。但我把它包括在內是為了好玩!
let csv = `id, category, year, color, price \n
01, SUV > AWD > Sport > Benz, 2017, blue, $20 \n
02, SUV > FWD > Family > Benz, 2018, black, $20 \n
03, Sedan > AWD > BNW, 2017, white, $30 \n
04, SUV > AWD > Sport > BNW, 2012, red, $30 \n
05, Sedan > RWD > Toyota, 2019, white, $30`;
const parseCsv = (csv) => {
const result = {};
// Parse/Sanitize all the data up front
const rows = csv
.split("\n")
.filter((row, index) => index && row)
.map((row) => {
const columns = row.split(",");
return {
headers: columns[1].split('>').map(p => p.trim()),
color: columns[3].trim()
};
});
// The recursive function, takes in
// {target} the current object in the tree we're at
// {headers} the remaining headers to step through
// {color} the color to assign to the last header
const handleRow = (target, headers, color) => {
// Last header, so just assign the color to it
if (headers.length === 1) {
target[headers[0]] = color;
return;
}
// Else we need to get or set another object level
const newTarget = (target[headers[0]] = target[headers[0]] ?? {});
// And call into the next level with it and the remaining headers
handleRow(newTarget, headers.slice(1), color);
};
for (const row of rows) {
handleRow(result, row.headers, row.color);
}
return result;
};
console.log(parseCsv(csv));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/315807.html
標籤:javascript 文件 目的 二叉树
上一篇:使用函式引數作為回傳物件中的鍵
