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

223
Visualizações
array.filter remove undefined value

I have two arrays. My first array is multidimensional. I am looping over the first array and checking if the index matches HeaderIndex value on an object in my second array. If so I am returning a new array with updated object. However I do not want my returned array to have any undefined values... I tried using array.filter

Here is my code below

const Array1 = [
  ['Alex', 'Boe', 'MeowWolf', 'pizza', 'pink', 'MeowWolf', 'corsair', 'coarse hair'],
  ['Arron', 'Coe', 'Kmart', 'tofu', 'purple', 'purr pull'],
  ['Jane', 'Doe', 'Sears', 'tacos', 'orange', 'Sears', 'see ears'],
  ['John', 'Eoe', 'YugiOh', 'blueberries', 'magenta'],
  ['Suzie', 'Boe', 'Toyota', 'steroids', 'blue']
]


const Array2 = [{
    header: 'First name',
    HeaderIndex: 0
  },
  {
    header: 'Last name',
    HeaderIndex: 1
  },
  {
    header: 'Company',
    HeaderIndex: 2
  },
  {
    header: 'Favorite food',
    HeaderIndex: 3
  },
  {
    header: 'Favorite color',
    HeaderIndex: 4
  },
]

const testResult = Array1.map(
  (arr) => arr.map(
    (string, index) => {
      if (Array2.filter(
          (obj) => obj.HeaderIndex === index)[0])
        return {
          "ChosenHeader": Array2.filter(
            (obj) => obj.HeaderIndex === index),
          "content": string
        }
    }))

console.log(testResult);

I am getting this result

0: (8) [{…}, {…}, {…}, {…}, {…}, undefined, undefined, undefined]
1: (6) [{…}, {…}, {…}, {…}, {…}, undefined]
2: (7) [{…}, {…}, {…}, {…}, {…}, undefined, undefined]
3: (5) [{…}, {…}, {…}, {…}, {…}]
4: (5) [{…}, {…}, {…}, {…}, {…}]

But I want something like this

0: (5) [{…}, {…}, {…}, {…}, {…}]
1: (5) [{…}, {…}, {…}, {…}, {…}]
2: (5) [{…}, {…}, {…}, {…}, {…}]
3: (5) [{…}, {…}, {…}, {…}, {…}]
4: (5) [{…}, {…}, {…}, {…}, {…}]

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I guess you need something like this:

const result = array1.map((arr1, index) => {
  if (array2.some((arr2) => arr2.HeaderIndex === index))
    return {
      ChosenHeader: array2.find(obj => obj.HeaderIndex === index),
      content: arr1,
    };
});
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