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

134
Vistas
Cómo filtrar a través de una matriz de objetos y enumerar todos los objetos que contienen una determinada palabra clave en uno de sus valores

Estoy trabajando en una serie de objetos de libros y la tarea es consolar. Registrar una lista de todos los libros que contienen "empresas" o "personas" en su título.

 const books = [{ title: 'How to win friends and influence people', authors: ['Dale Carnegie'], publisher: 'Pocket Books', year: '1936' }, { title: 'Management: tasks, responsibilities, practices', authors: ['Peter F. Drucker'], publisher: 'Harper Business', year: '1985' }, { title: 'Strength finder 2.0', authors: ['Tom Rath'], publisher: 'Gallup Press', year: '2007' }, { title: 'In search of excellence: Lessons from America\'s best-run companies', authors: ['Thomas Peters', ' Robert H. Waterman'], publisher: 'Harper Collins', year: '1982' }, { title: 'Built to last: Successful habits of visionary companies', authors: ['James C. Collins', 'Jerry I. Porras'], publisher: 'Harper Collins', year: '1994' }, { title: 'Reengineering the corporation: A manifesto for business revolution', authors: ['Michael Hammer', 'James A. Champy'], publisher: 'Harper Collins', year: '1993' }, { title: 'Competitive advantage: Creating and sustaining superior performance', authors: ['Michael E. Porter'], publisher: 'Free Press', year: '1998' }, { title: 'Crossing the chasm: Marketing and selling technology products to mainstream customers', authors: ['Geoffrey A. Moore', 'Regis McKenna'], publisher: 'Pocket Books', year: '1936' }, { title: '7 habits of highly effective people: Powerful lessons in personal change', authors: ['Stephen R. Covey'], publisher: 'Simon and Shuster', year: '1990' }, { title: 'The Six Sigma Way', authors: ['Peter S. Pande et al', 'Robert P. Neuman', 'Roland R. Cavanagh'], publisher: 'McGraw Hill', year: '2000' }, { title: 'The innovator\'s dilemma: When new technologies cause great firms to fail', authors: ['Clayton M. Christensen'], publisher: 'Harvard Business School Press', year: '1997' }, { title: 'The Essential Drucker', authors: ['Peter F. Drucker'], publisher: 'Harper Business', year: '2001' }];

Hasta ahora he intentado usar filter() e includes() como se indica a continuación, pero solo devuelve libros que contienen la palabra clave "personas".

let palabraclave = libros.filtro (títulolibro => títulolibro.título.includes('personas'||'empresas'));
consola.log(palabra clave);

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

0

Sugeriría usar Array.filter() junto con Array.some() para que coincida con el resultado deseado.

Creamos una matriz de palabras clave y luego devolvemos cualquier elemento que coincida con some de ellas.

 const books = [{ title: 'How to win friends and influence people', authors: ['Dale Carnegie'], publisher: 'Pocket Books', year: '1936' }, { title: 'Management: tasks, responsibilities, practices', authors: ['Peter F. Drucker'], publisher: 'Harper Business', year: '1985' }, { title: 'Strength finder 2.0', authors: ['Tom Rath'], publisher: 'Gallup Press', year: '2007' }, { title: 'In search of excellence: Lessons from America\'s best-run companies', authors: ['Thomas Peters', ' Robert H. Waterman'], publisher: 'Harper Collins', year: '1982' }, { title: 'Built to last: Successful habits of visionary companies', authors: ['James C. Collins', 'Jerry I. Porras'], publisher: 'Harper Collins', year: '1994' }, { title: 'Reengineering the corporation: A manifesto for business revolution', authors: ['Michael Hammer', 'James A. Champy'], publisher: 'Harper Collins', year: '1993' }, { title: 'Competitive advantage: Creating and sustaining superior performance', authors: ['Michael E. Porter'], publisher: 'Free Press', year: '1998' }, { title: 'Crossing the chasm: Marketing and selling technology products to mainstream customers', authors: ['Geoffrey A. Moore', 'Regis McKenna'], publisher: 'Pocket Books', year: '1936' }, { title: '7 habits of highly effective people: Powerful lessons in personal change', authors: ['Stephen R. Covey'], publisher: 'Simon and Shuster', year: '1990' }, { title: 'The Six Sigma Way', authors: ['Peter S. Pande et al', 'Robert P. Neuman', 'Roland R. Cavanagh'], publisher: 'McGraw Hill', year: '2000' }, { title: 'The innovator\'s dilemma: When new technologies cause great firms to fail', authors: ['Clayton M. Christensen'], publisher: 'Harvard Business School Press', year: '1997' }, { title: 'The Essential Drucker', authors: ['Peter F. Drucker'], publisher: 'Harper Business', year: '2001' }]; const keyWords = ['people', 'companies']; function findMatchingTitles(books, keyWords) { return books.filter(({title}) => { return keyWords.some(keyword => title.includes(keyword)); }) } console.log('Matching titles:'); const result = findMatchingTitles(books, ['people', 'companies']); result.forEach(book => console.log(book.title));
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

No se puede usar el operador OR dentro de include(). Entonces puedes escribir la lógica afuera.

 books.filter(book => book.title.includes('companies') || book.title.includes('people'))

Si la coincidencia debe ser insensible a mayúsculas y minúsculas:

 books.filter(book => { const titleLower = book.title.toLowerCase() return titleLower.includes('companies') || titleLower.includes('people') })
about 4 years ago · Juan Pablo Isaza Denunciar

0

puedes usar expresiones regulares:

 const peopleRegex = /people/i const companiesRegex = /companies/i const result = books.filter(item=>peopleRegex.test(item.title) || companiesRegex.test(item.title))

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