Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...