React中动态嵌套数组过滤promotion_type为multi_buy的数据
问题描述
React项目中需从动态更新的嵌套数组筛选包含promotion_type: "multi_buy"的子数组,此前依赖固定索引的过滤方法无效,现提供示例数据、无效代码及期望结果:
示例数组
const array = [ [ { "type": "metadata", "group_type": "metadata", "schema_version": "1.1.0" }, { "type": "metadata", "name": "invoice_identifier", "value": "DEV16708433389494MO" }, { "type": "metadata", "name": "ordered_at", "value": "2022-12-12 11:11:16" }, { "type": "metadata", "name": "billing_invoice_id", "value": "bd898352-8824-424a-b3bc-2409250b4e56" }, { "type": "metadata", "name": "unique_sku_count", "value": "1" }, { "type": "metadata", "name": "total_item_count", "value": "11" } ], [ { "type": "metadata", "group_type": "promotion", "promotion_type": "multi_buy", "promotion_element_name": "COCA COLA CANS 200 ml Classic - Buy 10 for $8.00" }, { "product_sku_id": 85, "type": "product", "name": "Diet 200ml Each", "name_docket": "", "variant_info": "1 Unit", "quantity": 10, "price_inclusive": 95, "total_exclusive": 864, "total_inclusive": 950, "discount_included": false }, { "type": "discount", "name": "* Promo: COCA COLA CANS 200 ml Classic - Buy 10 for $8.00", "quantity": 1, "price_inclusive": -150, "total_exclusive": -136, "total_inclusive": -150 } ], [ { "type": "metadata", "group_type": "surcharges" }, { "type": "shipping", "name": "Shipping Charge", "quantity": 1, "price_inclusive": 1500, "total_exclusive": 1364, "total_inclusive": 1500 } ], [ { "type": "metadata", "group_type": "totals" }, { "type": "tax_amount", "name": "GST", "amount": 72 }, { "type": "total", "name": "Total", "total_exclusive": 2228, "total_inclusive": 2300 } ], [ { "type": "metadata", "group_type": "other" }, { "type": "other", "name": "Total savings", "amount": 150 } ], [ { "type": "metadata", "group_type": "payments" }, { "type": "payment", "id": 2044, "identifier": "41a9a71a-e719-44e0-b35c-4731599cc413", "name": "Credit Card", "description": "Visa ending in 1111", "amount": 2300, "status": "success" } ] ]
无效代码
const soft = array[1]?.filter(({ categories }) => categories?.some(({ promotion_type }) => promotion_type === 'multi_buy') );
期望结果
[ { "type": "metadata", "group_type": "promotion", "promotion_type": "multi_buy", "promotion_element_name": "COCA COLA CANS 200 ml Classic - Buy 10 for $8.00" }, { "product_sku_id": 85, "type": "product", "name": "Diet 200ml Each", "name_docket": "", "variant_info": "1 Unit", "quantity": 10, "price_inclusive": 95, "total_exclusive": 864, "total_inclusive": 950, "discount_included": false }, { "type": "discount", "name": "* Promo: COCA COLA CANS 200 ml Classic - Buy 10 for $8.00", "quantity": 1, "price_inclusive": -150, "total_exclusive": -136, "total_inclusive": -150 } ]
错误原因
- 硬编码索引
array[1]依赖固定位置,动态数组中目标子数组位置可能变化,完全不可靠 - 错误访问了不存在的
categories属性,原数据结构中无此字段,逻辑完全偏离需求
正确实现方案
核心思路:遍历外层数组,筛选出**内部包含任意元素满足promotion_type === 'multi_buy'**的子数组。
场景1:仅需第一个符合条件的子数组
const targetSubArray = array.find(subArr => subArr.some(item => item.promotion_type === 'multi_buy') );
场景2:需所有符合条件的子数组(如存在多个multi_buy促销)
const allTargetSubArrays = array.filter(subArr => subArr.some(item => item.promotion_type === 'multi_buy') );
验证结果
执行上述代码后,targetSubArray会完全匹配你期望的结果。
内容的提问来源于stack exchange,提问作者rasif sahl
相关产品推荐
相关产品推荐

