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

200
Views
¿Cómo crear una nueva matriz a partir de otra?

Tengo una matriz:

 const animals = ['cat', 'dog', 'cow', 'horse']

Y segundo:

 const animalsWithNames = ['cat:mik', 'dog', 'horse:lucy']

Y necesito crear una función que me dé como resultado una tercera matriz:

 const result = ['cat', 'dog', 'horse']

Entonces, también entiendo que tengo que mapear la primera matriz y verificar por función. Si existe un fragmento de cadena en el elemento de la primera matriz, debería estar en el resultado. ¿Tengo razón? Por favor acerca de consejos o propuestas de solución.

Mi intento:

 const checkStr = str => animalsWithNames.forEach(el => { if (el.substring(0, str.length) === str) { return str } }) const result = animals.map(el => checkStr(el)) console.log(result)
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede intentar usar Array.prototype.filter() y Array.prototype.some() de la siguiente manera:

 const animals = ['cat', 'dog', 'cow', 'horse']; const animalsWithNames = ['cat:mik', 'dog', 'horse:lucy']; const result = animals.filter(a => animalsWithNames.some(an => an.split(':')[0] == a)); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Simplemente use Array.reduce con un bucle a través de la primera matriz y verifique que los nodos estén presentes en la segunda.

 const animals = ['cat', 'dog', 'cow', 'horse']; const animalsWithNames = ['cat:mik', 'dog', 'horse:lucy']; const result = animals.reduce((acc, curr) => { const node = animalsWithNames.find(item => item.indexOf(curr) > -1); if (node) { acc.push(curr) } return acc; }, []); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

 const animals = ['cat', 'dog', 'cow', 'horse'] const animalsWithNames = ['cat:mik', 'dog', 'horse:lucy'] const names = animalsWithNames.map(e => e.split(":")[0]) const result = names.filter(e => animals.includes(e)) console.log(result)

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!