循环对象对象以检查是否与我的变量相同的值

谁能告诉我解决这个js算法?我正在到达嵌套对象对象以检查与我的变量相同的类别值,如果为 true,则返回这些对象与值匹配。不确定我是否使用了正确的方法,但也不确定如何返回整个对象。

对象

const data = [
    {
      "id": 799,
      "name": "Ship Your Idea",
      "slug": "ship-your-idea-22",
      "permalink": "https://example.com/product/ship-your-idea-22/",
       "categories": [
        {
          "id": 9,
          "name": "Clothing",
          "slug": "clothing"
        }
      ]
    },
    {
      "id": 794,
      "name": "Premium Quality",
      "slug": "premium-quality-19",
      "permalink": "https://example.com/product/premium-quality-19/",
      "categories": [
        {
          "id": 14,
          "name": "T-shirts",
          "slug": "t-shirts"
        }
      ]
    },
    {
      "id": 795,
      "name": "Premium Quality2",
      "slug": "premium-quality-193",
      "permalink": "https://example.com/product/premium-quality-193/",
      "categories": [
        {
          "id": 15,
          "name": "Clothing",
          "slug": "clothing"
        }
      ]
    }
  ]

我的功能

 const brand = 'clothing';

  function fliterProductByCategorly(obj, brand){
    // 1.loop through each objects object
    // 2.loop through object and get categorie
    // 3.categorie filter get slug
    // 4.check if slug value are same value as global slug, if do only return object that match the brand value.

    return Object.entries(obj).filter(([, {categories}]) => Object.entries(categories).filter(([, {slug}]) => slug == brand ))
  }

  console.log(fliterProductByCategorly(data, brand))

示例: https://stackblitz.com/edit/js-pizeq4

stack overflow Looping Objects Object to check isit the value same as my variable
原文答案
author avatar

接受的答案

假设类别数组只有 1 个元素

const data = [
    {
      "id": 799,
      "name": "Ship Your Idea",
      "slug": "ship-your-idea-22",
      "permalink": "https://example.com/product/ship-your-idea-22/",
       "categories": [
        {
          "id": 9,
          "name": "Clothing",
          "slug": "clothing"
        }
      ]
    },
    {
      "id": 794,
      "name": "Premium Quality",
      "slug": "premium-quality-19",
      "permalink": "https://example.com/product/premium-quality-19/",
      "categories": [
        {
          "id": 14,
          "name": "T-shirts",
          "slug": "t-shirts"
        }
      ]
    },
    {
      "id": 795,
      "name": "Premium Quality2",
      "slug": "premium-quality-193",
      "permalink": "https://example.com/product/premium-quality-193/",
      "categories": [
        {
          "id": 15,
          "name": "Clothing",
          "slug": "clothing"
        }
      ]
    }
  ]

  const brand = 'clothing';

  function fliterProductByCategorly(obj, brand){
    return obj.filter(e => e.categories[0].slug===brand)
  }

  console.log(fliterProductByCategorly(data, brand))
.as-console-wrapper { max-height: 100% !important; top: 0; }

如果类别包含超过 1 个元素,则可能使用 reduce 实现。

const data = [
    {
      "id": 799,
      "name": "Ship Your Idea",
      "slug": "ship-your-idea-22",
      "permalink": "https://example.com/product/ship-your-idea-22/",
       "categories": [
        {
          "id": 9,
          "name": "Clothing",
          "slug": "clothing"
        }
      ]
    },
    {
      "id": 794,
      "name": "Premium Quality",
      "slug": "premium-quality-19",
      "permalink": "https://example.com/product/premium-quality-19/",
      "categories": [
        {
          "id": 14,
          "name": "T-shirts",
          "slug": "t-shirts"
        }
      ]
    },
    {
      "id": 795,
      "name": "Premium Quality2",
      "slug": "premium-quality-193",
      "permalink": "https://example.com/product/premium-quality-193/",
      "categories": [
        {
          "id": 16,
          "name": "T-shirts",
          "slug": "t-shirts"
        },
        {
          "id": 15,
          "name": "Clothing",
          "slug": "clothing"
        }
      ]
    }
  ]

  const brand = 'clothing';

  function fliterProductByCategorly(obj, brand){
    return obj.reduce((acc,curr) => {
        if (curr.categories.filter(e => e.slug===brand).length>0){
        acc.push(curr)
      }
      return acc;
    },[])
  }

  console.log(fliterProductByCategorly(data, brand))
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案: