Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
how to remove undefined values from array with javascript

i have tried with filter method but it removes all values. i'm getting this undefined value because of this function. I want to merge these three arrays and remove only undefined.

Example Arrays values:

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']

i have this

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 Respostas
Responde à pergunta

0

Map through the array and remove all occurrences of undefined, then trim the result:

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 Relatório

0

you can write it like this

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

0

The other answers don't discriminate between the lack of a value (undefined) at an array element vs having the literal string "undefined" at an array element.

I've explained how to accommodate for that using comments in the code below:

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda