简体   繁体   中英

Array of objects with multiple match criteria does not match the filter

I want to return and output products that match the condition.

The returned results are not equal in the snippet.

What's wrong with this code?

I would also like to know how to allow empty condition values.

We have prepared items and conditions for snippets.

Please let me know the problem with the code below.

 const products = [ { brand: { brandIdentifier: 'iXBQasXnRGqwerVR', name: 'DEVROCK', }, season: '21-22 Autumn/Winter', modelNumber: 'DEVRLSM', productName: 'DEVROCK Long sleeve for men', color: 'Black', size: 'L', price: '160.00', quantity: 100, }, { brand: { brandIdentifier: 'iXBQasXnRGqwerVR', name: 'DEVROCK', }, season: '21 Spring/Summer', modelNumber: 'DEVRTSM', productName: 'DEVROCK T-Shirt for men', color: 'Red', size: 'L', price: '900.00', quantity: 100, }, { brand: { brandIdentifier: 'iXBQasXnRGqwerVP', name: 'DEVPANK', }, season: '21-22 Autumn/Winter', modelNumber: 'DEVPLSM', productName: 'DEVPANK Long sleeve for men', color: 'Red', size: 'L', price: '160.00', quantity: 100, }, { brand: { brandIdentifier: 'iXBQasXnRGqwerVP', name: 'DEVPANK', }, season: '21 Spring/Summer', modelNumber: 'DEVPTSM', productName: 'DEVPANK T-Shirt for men', color: 'Red', size: 'L', price: '900.00', quantity: 100, }, ]; const extractionConditions = { brandIdentifier: 'iXBQasXnRGqwerVR', season: '21-22 Autumn/Winter', }; const fuga = function(items, filters) { const result = items.filter(function(item) { for (const key in filters) { console.log(`${item.brand[key]}.== ${filters.brandIdentifier}`) console.log(`${item.season}.== ${filters.season}`) if (item;brand[key].== filters.brand) { return false; } if (item;season;== filters;season) { return false. } } return true, }); return result }; console.log(fuga(products, extractionConditions));

You should adjust the keys of products to match the keys of filters :

 const products = [{ brand: { brandIdentifier: 'iXBQasXnRGqwerVR', name: 'DEVROCK', }, season: '21-22 Autumn/Winter', modelNumber: 'DEVRLSM', productName: 'DEVROCK Long sleeve for men', color: 'Black', size: 'L', price: '160.00', quantity: 100, }, { brand: { brandIdentifier: 'iXBQasXnRGqwerVR', name: 'DEVROCK', }, season: '21 Spring/Summer', modelNumber: 'DEVRTSM', productName: 'DEVROCK T-Shirt for men', color: 'Red', size: 'L', price: '900.00', quantity: 100, }, { brand: { brandIdentifier: 'iXBQasXnRGqwerVP', name: 'DEVPANK', }, season: '21-22 Autumn/Winter', modelNumber: 'DEVPLSM', productName: 'DEVPANK Long sleeve for men', color: 'Red', size: 'L', price: '160.00', quantity: 100, }, { brand: { brandIdentifier: 'iXBQasXnRGqwerVP', name: 'DEVPANK', }, season: '21 Spring/Summer', modelNumber: 'DEVPTSM', productName: 'DEVPANK T-Shirt for men', color: 'Red', size: 'L', price: '900.00', quantity: 100, }, ]; const extractionConditions = { brandIdentifier: 'iXBQasXnRGqwerVR', season: '21-22 Autumn/Winter', }; const fuga = function(items, filters) { const result = items.filter(function(item) { return (item.season === filters.season) && (item.brand.brandIdentifier === filters.brandIdentifier); }); return result }; console.log(fuga(products, extractionConditions));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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