【问题标题】:Javascript return object inside for loopHi guyJavascript在for循环中返回对象嗨伙计
【发布时间】:2021-04-05 10:32:32
【问题描述】:

谁能指导我如何在 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,我正在尝试遍历总计以获取键 (X) 值和订单 (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"
            }
        ]
    }
}

【问题讨论】:

  • 您能否向我们提供您想要获取的原始对象和输出对象?
  • 是的,我刚刚添加了原始的json。谢谢
  • 这毫无意义 - 将地图映射到另一个,但您正在循环并试图“返回”每个?你期望结果是什么?此方法只有一个输出,因此您需要合并到一个对象或数组中。
  • @Lee 您要查找的所需输出对象是什么?
  • 是的,对 JS 来说很抱歉,我基本上想遍历返回键(日期)和订单值的“总计”。我无法映射,因为它不是数组,所以不确定如何获取这些值,因为键会改变我假设我不应该使用 orders['totals']['2020-03'] 等等。

标签: javascript reactjs loops for-loop object


【解决方案1】:
const chartData = Object.entries(data['totals']).map(([date, values]) => {
    return {x: date, y: values.orders}
  })

data 是您尝试循环的 JSON。

【讨论】:

  • 非常感谢,这正是我需要的
猜你喜欢
  • 2016-12-30
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多