Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
Objetos de bucle Objeto para comprobar si tiene el mismo valor que mi variable

¿Alguien puede mostrarme cómo resolver este algoritmo js? Estoy llegando al objeto de objetos anidados para verificar si el valor de las categorías es el mismo que mi variable y, si es cierto, devuelve esos objetos que coinciden con el valor. No estoy seguro de si estoy usando la forma correcta, pero tampoco estoy seguro de cómo devolver todos los objetos.

objetos

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

mis funciones

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

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Suponiendo que la matriz de categorías tiene solo 1 elemento

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

Posible implementación usando reducir si las categorías contienen más de 1 elemento.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!