Javascript为loopHi-goy返回内部对象



有人能指导我如何在for循环中返回对象吗?它目前在第一次迭代时退出,我知道这里不应该使用return,但不确定我应该使用什么。

const allOrders = orderTotals && orderTotals.map((orders) => {

let obj = orders['totals']
for (const key of Object.keys(obj)) {
return {
x: key,
y: obj[key].orders,
}
}
});

谢谢

--

编辑

这是json,我试图循环遍历总数以获得key(X(值和orders(y(值,这样我就可以创建一个图表。

{
"total_sales": "1410.37",
"net_sales": "1404.47",
"average_sales": "117.04",
"total_orders": 66,
"total_items": 65,
"total_tax": "0.00",
"total_shipping": "5.90",
"total_refunds": 48.67999999999999971578290569595992565155029296875,
"total_discount": "60.25",
"totals_grouped_by": "month",
"totals": {
"2020-01": {
"sales": "0.00",
"orders": 0,
"items": 0,
"tax": "0.00",
"shipping": "0.00",
"discount": "0.00",
"customers": 0
},
"2020-02": {
"sales": "75.37",
"orders": 3,
"items": 3,
"tax": "0.00",
"shipping": "0.00",
"discount": "3.00",
"customers": 2
},
"2020-03": {
"sales": "55.23",
"orders": 2,
"items": 2,
"tax": "0.00",
"shipping": "0.00",
"discount": "5.75",
"customers": 1
},
"2020-04": {
"sales": "0.00",
"orders": 0,
"items": 0,
"tax": "0.00",
"shipping": "0.00",
"discount": "0.00",
"customers": 2
},
"2020-05": {
"sales": "71.37",
"orders": 2,
"items": 2,
"tax": "0.00",
"shipping": "0.00",
"discount": "4.60",
"customers": 1
},
"2020-06": {
"sales": "96.21",
"orders": 5,
"items": 5,
"tax": "0.00",
"shipping": "0.00",
"discount": "5.75",
"customers": 0
},
"2020-07": {
"sales": "85.86",
"orders": 4,
"items": 4,
"tax": "0.00",
"shipping": "5.90",
"discount": "0.00",
"customers": 4
},
"2020-08": {
"sales": "187.77",
"orders": 8,
"items": 8,
"tax": "0.00",
"shipping": "0.00",
"discount": "5.75",
"customers": 1
},
"2020-09": {
"sales": "112.35",
"orders": 5,
"items": 5,
"tax": "0.00",
"shipping": "0.00",
"discount": "4.60",
"customers": 2
},
"2020-10": {
"sales": "302.08",
"orders": 14,
"items": 14,
"tax": "0.00",
"shipping": "0.00",
"discount": "2.80",
"customers": 4
},
"2020-11": {
"sales": "442.82",
"orders": 22,
"items": 21,
"tax": "0.00",
"shipping": "0.00",
"discount": "6.00",
"customers": 10
},
"2020-12": {
"sales": "29.99",
"orders": 1,
"items": 1,
"tax": "0.00",
"shipping": "0.00",
"discount": "0.00",
"customers": 0
}
},
"total_customers": 27,
"_links": {
"about": [
{
"href": "/wp-json/wc/v3/reports"
}
]
}
}
const chartData = Object.entries(data['totals']).map(([date, values]) => {
return {x: date, y: values.orders}
})

其中data是您试图循环的JSON。

最新更新