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

109
Visualizações
JavaScript Array Map Function not working as expected

I have got an issue with the Array.map function not working as I would expect. I have simplified my example below, I just want to get an understanding of where I am going wrong.

So I have the following code below:

console.log(this.reportTestData)
let data = this.reportTestData.map((row) => {
    return [ 1 , 2, 3]
});
console.log(data)
return data

From the first console.log, this.reportTestData is an array containing 92 objects, the content of each object is irrelevant:

enter image description here

So from the code, I would expect the map function to run over the array 92 times, and return a new array ( [1,2,3] ) for each element. Leaving me with an array of 92 elements, which each element is a [1,2,3] array. However that is not what I get.

enter image description here

Instead I am getting an array of 92 elements, but each element is completely empty.

I also tried to return objects from the map function instead:

 console.log(this.reportTestData)
 let data = this.reportTestData.map((col) => {
     return { test : 1 }
 });
 console.log(data)
 return data

However I still get empty objects returned with no properties:

enter image description here

Any help that could be offered would be greatly appreciated, as I cannot see where I am making a mistake.

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

0

Works for me

const reportTestData = new Array(12).fill(0);
let data1 = reportTestData.map((row) => {
    return [ 1 , 2, 3]
});
console.log(data1)

let data2 = reportTestData.map((row) => {
    return { test : 1 }
});
console.log(data2)

enter image description here

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório

0

I tried for 10 items in an array, and as you may see there are 10-pieces of [1,2,3] ... so map function is working fine, as par I can say.

let reportTestData = [1,2,3,4,5,6,7,8,9,10]
console.log(reportTestData)
let data = reportTestData.map((item) => [ 1 , 2, 3]);
console.log(data)

Now trying again with 10-objects in an array:

let reportTestData = [{},{},{},{},{},{},{},{},{},{}]
console.log(reportTestData)
let data = reportTestData.map((item) => [ 1 , 2, 3]);
console.log(data)

Again map function of Arrays works fine, so you need to check what wrong you are doing in your code.

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