我有一個按以下方式格式化的陣列陣列:
[['BTC', '2022-04-27', '39151.53'],
['BTC', '2022-04-26', '37730.20'],
['BTC', '2022-04-25', '39151.53'],
['ETH', '2022-04-27', '39151.53'],
['ETH', '2022-04-26', '37730.20'],
['ETH', '2022-04-25', '39151.53']]
我想結束的是:
[['Date', 'BTC, 'ETH'],
['2022-04-27', '39151.53', '39151.53'],
['2022-04-26', '37730.20', '39151.53'],
['2022-04-25', '39151.53', '39151.53']]
有沒有簡單的方法來解決它?
uj5u.com熱心網友回復:
這里使用另一個解決方案reduce()。
這首先創建一個 JavaScript 物件,該物件將允許在程式中快速查找給定貨幣和日期的特定值,然后轉換該物件以創建 OP 請求的結果。例如,當給定貨幣和日期有兩個值時,它也會因輸入資料不正確而引發錯誤。
const input = [
["BTC", "2022-04-27", "39151.53"],
["BTC", "2022-04-26", "37730.20"],
["BTC", "2022-04-25", "39151.53"],
["ETH", "2022-04-27", "39151.53"],
["ETH", "2022-04-26", "37730.20"],
["ETH", "2022-04-25", "39151.53"],
];
function pivot(input){
return input.reduce((perDay, [currency, date, value]) => {
if(perDay[date]) {
// we already have a value for this day
if(perDay[date][currency]){
// we already have a value for this currency and day => error
throw new Error(`Two values for for the same day. Currency: ${currency}, Day: ${date}`);
}
// add value for another currency for that day
else perDay[date][currency] = value;
} else perDay[date] = { [currency]: value };
return perDay;
}, {})
}
// result as a JS object which will allow quick lookups
const pivotted = pivot(input);
console.log("Result as JavaScript object")
console.log(pivotted);
// result transformed to your expected result
const result = Object.entries(pivotted).map(([date, values]) => [date, ...Object.values(values)]);
result.unshift(['Date', 'BTC', 'ETH']);
console.log("Result as JavaScript nested array")
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
uj5u.com熱心網友回復:
看起來 OP 需要由日期欄位索引的內部陣列。這可以通過減少來完成。一旦有了索引,遍歷鍵(日期)并收集與每個日期關聯的值。
const array = [['BTC', '2022-04-27', '39151.53'],
['BTC', '2022-04-26', '37730.20'],
['BTC', '2022-04-25', '39151.53'],
['ETH', '2022-04-27', '39151.53'],
['ETH', '2022-04-26', '37730.20'],
['ETH', '2022-04-25', '39151.53']];
const dateIndex = array.reduce((acc, a) => {
let date = a[1];
if (!acc[date]) acc[date] = [];
acc[date].push(a);
return acc;
}, {});
let result = [['Date', 'BTC', 'ETH']];
Object.keys(dateIndex).map(date => {
let values = dateIndex[date].map(arr => arr[2]);
result.push([date, ...values]);
});
console.log(result)
uj5u.com熱心網友回復:
您可以使用具有列型別和目標索引的物件。然后找到該行并將型別分配給所需的單元格。
const
data = [['BTC', '2022-04-27', '39151.53'], ['BTC', '2022-04-26', '37730.20'], ['BTC', '2022-04-25', '39151.53'], ['ETH', '2022-04-27', '39151.53'], ['ETH', '2022-04-26', '37730.20'], ['ETH', '2022-04-25', '39151.53']],
types = { Date: 0, BTC: 1, ETH: 2 },
result = data.reduce((r, [type, date, value]) => {
let row = r.find(a => a[types.Date] === date);
if (!row) r.push(row = [date, ...Array(Object.keys(types).length - 1).fill('0')]);
row[types[type]] = value;
return r;
}, [Object.keys(types)]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/466257.html
標籤:javascript 数组
上一篇:資料透視表與公式Google表格
