Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
Looping Objects Object to check isit the value same as my variable

can anyone show me on solve this js algorithm ? i am reaching nested objects object to check isit the categories value same as my variable and if true return those object match the value back. not sure if i am using the right way, but also not sure how to return the whole objects back.

objects

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"
        }
      ]
    }
  ]

my functions

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

sample : https://stackblitz.com/edit/js-pizeq4

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Assuming that categories array has only 1 element

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

Possible implementation using reduce if categories contains more than 1 element.

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda