You need to enable JavaScript to run this app. React中按对象数组order_status值提取对应列数据显示异常修复
优惠活动
大模型
产品
解决方案
定价
更多

React中按对象数组order_status值提取对应列数据显示异常修复

问题原因

你的代码核心逻辑存在错误:你仅用some方法判断了数组中是否存在对应状态的订单,判断通过后直接遍历了完整的billInfo数组,没有筛选出和当前列匹配状态的订单,所以只要对应状态存在,该列就会把所有订单数据都渲染出来,自然就出现了所有列展示相同全量数据的问题。
同时你的代码存在大量冗余逻辑,7个状态列的渲染逻辑完全一致,后续维护成本很高。

修改方案

方案1:最小改动修复逻辑

只需要把每个列中遍历数组的逻辑,先加一层过滤,筛选出匹配当前列状态的订单再遍历即可,同时可以去掉多余的some判断:
比如「处理中订单」列的逻辑修改为:

<div>
  <h5 className="text-gray-500 capitalize">Processing Orders</h5>
  {billInfo ? (
    billInfo
      .filter((el) => el.order_status === "processing")
      .map((order, index) => (
        <div key={index}>
          <h3 className="font-medium text-xl">
            ৳ {order.order_amount || "N/A"}
          </h3>
          <p className="text-gray-500">
            Quantity: 
            <strong className="font-semibold text-black">
              {order.order_count || "N/A"}
            </strong>
          </p>
        </div>
      ))
  ) : "N/A"}
</div>

其余6个列按照同样的逻辑修改,把filter里的order_status值替换为对应列的状态即可。

方案2:优化冗余代码(推荐)

可以把所有状态列的配置抽成数组,批量渲染,代码更简洁易维护:
首先在组件内部定义状态配置:

const statusConfig = [
  { status: 'total', label: 'Total Orders' },
  { status: 'paid', label: 'Paid Orders' },
  { status: 'processing', label: 'Processing Orders' },
  { status: 'picked', label: 'Picked Orders' },
  { status: 'refunded', label: 'Refunded Orders' },
  { status: 'shipped', label: 'Shipped Orders' },
  { status: 'delivered', label: 'Delivered Orders' },
]

然后把原来的7个重复列的代码替换为遍历这个配置的逻辑:

<div className="grid grid-cols-5 gap-4 p-6">
  {statusConfig.map(({ status, label }) => {
    const matchOrders = billInfo?.filter(el => el.order_status === status) || []
    return (
      <div key={status}>
        <h5 className="text-gray-500 capitalize">{label}</h5>
        {matchOrders.length > 0 ? (
          matchOrders.map((order, index) => (
            <div key={index}>
              <h3 className="font-medium text-xl">
                ৳ {order.order_amount || "N/A"}
              </h3>
              <p className="text-gray-500">
                Quantity: 
                <strong className="font-semibold text-black">
                  {order.order_count || "N/A"}
                </strong>
              </p>
            </div>
          ))
        ) : "N/A"}
      </div>
    )
  })}
</div>

内容的提问来源于stack exchange,提问作者ashfaqrafi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 06:39:04