Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda