具有多个匹配条件的对象数组与筛选器不匹配



我想返回并输出符合条件的产品。

返回的结果在代码段中不相等。

这个代码出了什么问题?

我还想知道如何允许空的条件值。

我们已经为片段准备了项目和条件。

请让我知道下面代码的问题。

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));

您应该调整products的键以匹配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));

最新更新