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

198
Views
cómo eliminar valores indefinidos de la matriz con javascript

Lo he intentado con el método de filtro pero elimina todos los valores. Obtengo este valor indefinido debido a esta función. Quiero fusionar estas tres matrices y eliminar solo lo indefinido.

Valores de matrices de ejemplo:

 let arry1= ["txt", "txt2"]; let arry2= ["txt"]; let arry3= ["txt", "txt5", "txt6"]
 let c = arry1.map(function(value, index) { return `${value} ${arry2[index]} ${arry3[index]}` ; }); c =  ['txt txt txt', 'txt2 undefined undefined']

tengo esto

 let a = ["undefined this", "that undefined ", "undefined value undefined"] // i want this let a = ["this", "that", "value"] ```
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Asigne a través de la matriz y elimine todas las apariciones de undefined , luego recorte el resultado:

 let a = ["undefined this", "that undefined ", "undefined value undefined"] const result = a.map(e => e.replaceAll("undefined", "").trim()) console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

puedes escribirlo así

 let a=arry1.map(function(value, index) { value=`${value} ${arry2[index]||""}`.trim(); return `${value} ${arry3[index]||""}`.trim() ; });
about 4 years ago · Juan Pablo Isaza Report

0

Las otras respuestas no discriminan entre la falta de un valor ( undefined ) en un elemento de matriz frente a tener la cadena literal "undefined" en un elemento de matriz.

He explicado cómo acomodar eso usando comentarios en el siguiente código:

 const array1 = ["txt", "txt2"]; const array2 = ["txt"]; const array3 = ["txt", "txt5", "txt6"]; // Collect the arrays into another array so that we can iterate over them: const arrays = [array1, array2, array3]; // Create an array of the lengths of the arrays: const arrayLengths = arrays.map(({length}) => length); // Get the maximum value from the lengths: const maxLength = Math.max(...arrayLengths); const result = []; // One outer loop for each index of the longest array: for (let i = 0; i < maxLength; i += 1) { // A placeholder string: let str = ''; // One inner loop for each of the arrays: for (const array of arrays) { // The value at the current index of the current array: const strOrUndefined = array[i]; // If it's actually a string, then append a space and the string // to the placeholder string: if (typeof strOrUndefined === 'string') str += ` ${strOrUndefined}`; } // Push the placeholder string into the result array, // but first remove the extra space created by the first iteration: result.push(str.trimStart()); } console.log(result); // ["txt txt txt", "txt2 txt5", "txt6"]

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!