递归过滤对象数组:基于指定产品数据结构的技术问询
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
productFilterto() => true - Keep products with empty SKU lists: Remove the
filteredSKUs.length > 0check - Deeper nested structures: If your SKUs have their own nested arrays (e.g., variants), add a recursive filter step inside the
skuFilterlogic. 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

