简体   繁体   English

如何在 javascript 中映射 JSON 数据时处理 null 值

[英]how to handle null values while mapping JSON data in javascript

I have to map through a data looking similar to this,我必须通过看起来类似于此的数据 map,

"data": {
    "16450603807212420": [
        {
            "mall_id": 4,
             ... more entries
            "order_data": [
                {
                    "detail_order_id": 464,
                    "order_id": "16450603807212420",
                    "order_status": 200,
                    "goods_id": 1000000019,
                       ... more entries
                },
                {
                    "detail_order_id": 465,
                     ... more entries
                }
            ]
        },
        {
            "mall_id": 4,
              ... more entries
            "order_data": [
                {
                    "detail_order_id": 466,
                     ... more entries
                }
            ]
        }
    ],
    "16450603807212421": [null],
    "16450603807212422": [
        {
            "mall_id": 4,
               ... more entries
            "order_data": [
                {
                    "detail_order_id": 467,
                    ... more entries
                },
                {
                    "detail_order_id": 468,
                   ... more entries
                }
            ]
        }
    ],
    "16450603807212423": [
        {
            "mall_id": 4,
               ... more entries
            "order_data": [
                {
                    "detail_order_id": 467,
                    ... more entries
                },
                {
                  null
                }
            ]
        }
    ]
}

and my map Function looks like this我的 map Function 看起来像这样

 <div>
          {orderValues?.map((data, idx) => (
            <div key={idx}>
              {data?.map((order, index) => (
                <div key={index}>
                  <OrderListProduct
                    date={order.created_at}
                    orderType={order.delivery_option_name}
                    product_info={order.order_data}
                  />
                </div>
              ))}
            </div>
          ))}
        </div>

in order to map the data inside each key value of "16450----" which is an order Id.为了map里面的数据每个键值为“16450----”这是一个订单Id。 Im not supposed to have null inside the data but some error occurred and received the above looking data which my map function was not able to handle.我不应该在数据中包含 null 但发生了一些错误并收到了上面看起来的数据,我的 map function 无法处理。 The issue of the data containing null values was fixed from the back-end side but I would still like my map function to be able to handle whenever I receive null data.包含 null 值的数据问题已从后端修复,但我仍然希望我的 map function 能够在我收到 null 数据时处理。

help would be much appreciated!!!帮助将不胜感激!!!

You can use if in map, or filter them beforehand.您可以使用 map 中的 if,或者预先过滤它们。 Also optional changing operator?.还可选更改运算符?。

<div>
          {orderValues?.map((data, idx) => (
            <div key={idx}>
              {data?.map((order, index) => {
                if(!order) { ... }
                else {
                return (
                <div key={index}>
                  <OrderListProduct
                    date={order?.created_at}
                    orderType={order?.delivery_option_name}
                    product_info={order?.order_data}
                  />
                </div>
                )}}
              )}
            </div>
          ))}
        </div>

just check for null or undefined values before your map function's return statement只需在 map 函数的返回语句之前检查 null 或未定义的值

        <div>
          {orderValues?.map((data, idx) => (
            <div key={idx}>
              {data?.map((order, index) => order && (
                <div key={index}>
                  <OrderListProduct
                    date={order.created_at}
                    orderType={order.delivery_option_name}
                    product_info={order.order_data}
                  />
                </div>
              ))}
            </div>
          ))}
        </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM