下午好。我想显示一年中的月份以及在每个类别中花费的金额。前任:
{Month: "January", Food: 610, foodColor: "#063951", Others: 121, othersColor: "#C13018", …}
Food: 610
Health: 233
Month: "January"
Others: 121
Transport: 30
foodColor: "#063951"
healthColor: "#2BC4A9"
othersColor: "#C13018"
transportColor: "#0D95BC"
我从我的服务器获取所有月份的对象数组。像这样的东西:
(43) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "September", category: "Food", price: 610, color: "#063951"}
1: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "September", category: "Health", price: 233, color: "#2BC4A9"}
2: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "September", category: "Transport", price: 30, color: "#0D95BC"}
3: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "September", category: "Others", price: 121, color: "#C13018"}
4: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "October", category: "Health", price: 113, color: "#2BC4A9"}
5: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "October", category: "Transport", price: 330, color: "#0D95BC"}
6: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "October", category: "Others", price: 650, color: "#C13018"}
7: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "October", category: "Food", price: 170.55, color: "#063951"}
我创建了一个过滤月份的函数:
const getMonth = (month) => {
let monthData = totalTransactionsPerMonth.filter((transaction) =>{
return transaction.monthname === `${month}`
})
return monthData;
}
我用它来分别获取月份:
let janData = getMonth("January");
let fevData = getMonth("February");
let marData = getMonth("March");
let aprData = getMonth("April");
let mayData = getMonth("May");
let junData = getMonth("June");
let julData = getMonth("July");
let augData = getMonth("August");
let sepData = getMonth("September");
let octData = getMonth("October");
let novData = getMonth("November");
let decData = getMonth("December");
当我调用 console.log(janData) 我看到这个:
[{…}, {…}, {…}, {…}]
0: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "January", category: "Health", price: 233, color: "#2BC4A9"}
1: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "January", category: "Transport", price: 30, color: "#0D95BC"}
2: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "January", category: "Others", price: 121, color: "#C13018"}
3: {id: "zxFBMv43ZGgHJpd6a3tjyTO3Rxg1", monthname: "January", category: "Food", price: 610, color: "#063951"}
我想从包含月份数据的数组中(janData、fevData 等)创建一个像这样的对象:
const January = {
month:
food:
foodColor:
others:
othersColor:
transport:
transportColor:
health:
healthColor:
}
并且这些值将由数组的值填充。我试图创建一个函数来接收monthData作为参数,然后使用索引(例如:food:monthArgument [3] .price),但我得到“无法读取未定义的价格”。我进行了一些搜索,但找不到回复。拜托,我被困在这里,有人可以帮助我吗?
这个问题似乎并不重要,但我正在使用 React、Node、mySQL、Nivo/barChart