You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

递归过滤对象数组:基于指定产品数据结构的技术问询

Recursive Filtering for Product Array with Nested SKUs

Alright, let's break down how to implement recursive filtering for your product array. The key here is to handle both the top-level product objects and their nested products_SKU arrays—plus, we want the solution to be flexible so you can adjust the filtering criteria easily.

Step 1: Define Your Filter Criteria

First, let's separate the filtering logic from the recursion itself. This makes it super easy to tweak what you're filtering for later. Let's use example criteria to illustrate:

  • Keep only products from seller 'a'
  • Keep only SKUs with color 'Black'
// Filter condition for top-level products
const shouldKeepProduct = (product) => product.products_seller === 'a';

// Filter condition for nested SKUs
const shouldKeepSKU = (sku) => sku.productSKU_color === 'Black';

Step 2: Implement the Recursive Filter Function

We'll use reduce() to iterate over the products array. For each product, we first filter its SKU list, then decide whether to keep the product (based on your criteria and whether it has remaining SKUs, if that matters to you).

const filterProductsRecursively = (products, productFilter, skuFilter) => {
  return products.reduce((filteredList, currentProduct) => {
    // First, filter the nested SKUs for the current product
    const filteredSKUs = currentProduct.products_SKU.filter(skuFilter);

    // Decide if we keep this product:
    // - It passes the product filter
    // - It has at least one remaining SKU (adjust this if you want to keep products with empty SKU lists)
    if (productFilter(currentProduct) && filteredSKUs.length > 0) {
      // Create a copy of the product to avoid modifying the original data
      filteredList.push({
        ...currentProduct,
        products_SKU: filteredSKUs
      });
    }

    return filteredList;
  }, []);
};

Step 3: Use the Function with Your Data

Let's test this with your sample product array:

// Your original product data
const products = [
  {
    products_number: 14,
    products_ID: 'lvs_jeans-man',
    products_seller: 'a',
    products_SKU: [
      { productSKU_ID: 'nfl_lvs_jeans-man_xl_bl-stripe', productSKU_size: 'XL', productSKU_color: 'Blue_White' },
      { productSKU_ID: 'nfl_lvs_jeans-man_xl_bk', productSKU_size: 'XL', productSKU_color: 'Black' }
    ]
  },
  {
    products_number: 15,
    products_ID: 'lvs_jeans-man',
    products_seller: 'b',
    products_SKU: [
      { productSKU_ID: 'nfl_lvs_jeans-man_xl_bk', productSKU_size: 'XL', productSKU_color: 'Black' }
    ]
  }
];

// Run the filter
const filteredProducts = filterProductsRecursively(products, shouldKeepProduct, shouldKeepSKU);

console.log(filteredProducts);

Output:

[
  {
    products_number: 14,
    products_ID: 'lvs_jeans-man',
    products_seller: 'a',
    products_SKU: [
      { productSKU_ID: 'nfl_lvs_jeans-man_xl_bk', productSKU_size: 'XL', productSKU_color: 'Black' }
    ]
  }
]

Adjustments for Different Scenarios

  • No product filtering: If you only need to filter SKUs, set productFilter to () => true
  • Keep products with empty SKU lists: Remove the filteredSKUs.length > 0 check
  • Deeper nested structures: If your SKUs have their own nested arrays (e.g., variants), add a recursive filter step inside the skuFilter logic. For example:
    const filterSKUWithVariants = (sku) => {
      const filteredVariants = sku.productSKU_variants?.filter(variantFilter) || [];
      return shouldKeepSKU(sku) && filteredVariants.length > 0; // Adjust based on your needs
    };
    

This approach keeps your code clean, reusable, and easy to modify as your filtering requirements change.

内容的提问来源于stack exchange,提问作者nfl-x

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:26:15