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

205
Views
Recorra la matriz de objetos, si existe valor, devuelva otro valor

Con la información a continuación, estoy intentando recorrer cards , si hay un objeto helper anidado, devolver el title de ese objeto. Pero estoy recibiendo indefinidos o errores. Estaba pensando que tal vez reducir sería viable aquí.

Matriz de muestra:

 cards: [ 0: { title: 'Something', logo: 'logo link here', }, 1: { title: 'Something 2', logo: 'logo link here', helper: { text: 'helper text', }, }, 2: { title: 'Something 3', logo: 'logo link here', }, ]

código:

 cards.filter((item) => { if (item.helper) setHelperActive({...helperActive, item.title: true}) })
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

 let cards = [ { title: 'Something', logo: 'logo link here', }, { title: 'Something else', logo: 'logo link here', helper: { text: 'helper text', }, }, { title: 'Something', logo: 'logo link here', }, { title: 'Lorem Epsum', logo: 'logo link here', helper: { text: 'helper text', }, } ] let filtered = [] for(let i = 0; i < cards.length; i++) { if(cards[i].helper) { filtered.push(cards[i].title) } } console.log(filtered);

about 4 years ago · Juan Pablo Isaza Report

0

la solución es usar el mapa, no el filtro, debería ser así: var titles = cards.map(card=>{ return card.title })

about 4 years ago · Juan Pablo Isaza Report

0

Usando .filter() y verificando si el objeto tiene un accesorio llamado helper . En caso de que varios objetos coincidan con los criterios, sus títulos se unirán como una cadena separada por comas.

Retazo

 let cards = [ { title: 'Something', logo: 'logo link here', }, { title: 'Something 2', logo: 'logo link here', helper: { text: 'helper text', }, }, { title: 'Something', logo: 'logo link here', }, { title: 'Something 4', logo: 'logo link here', helper: { text: 'helper text', }, }, ] // to list titles from all matching objects as a comma-separated string console.log(cards.filter(ob => 'helper' in ob).map(({ title }) => title).join()); // suppose only the first matched object's title is required console.log(cards.find(ob => 'helper' in ob)?.title ?? 'no match found');

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!