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

188
Views
¿Existe una manera eficiente de obtener todos los valores de un determinado tipo de una matriz en JavaScript?

Supongamos que tengo una matriz sin ningún orden en particular y quiero obtener todos los valores de un tipo dado de esa matriz (para este ejemplo, usemos cadenas).

 oldArray = [1, "2", {3: 4}, 5, "6", /7/]; /* ... */ newArray = ["2", "6"];

Lógicamente, haría algo como esto:

 newArray = []; oldArray.forEach((element) => { if (typeof element === "string") { newArray.push(element); } });

(Aunque no es tan elegante como el one-liner de Python [value for value in oldArray if type(value) == str] , todavía es suficiente para mí).


Mi pregunta es: ¿hay una forma más eficiente de hacer esto o es una solución óptima?

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

0

Usando Array#filter y typeof :

 const oldArray = [1, "2", {3: 4}, 5, "6", /7/]; const newArray = oldArray.filter(e => typeof e === 'string'); console.log(newArray);

about 4 years ago · Juan Pablo Isaza Report

0

Puedes usar array.filter() :

 newArray = oldArray.filter(e => typeof e == "string")
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!