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

196
Visualizações
How we made array of object from array of array in javascript?

Here I am trying to convert array of array to array of object.

Below is my code -

let arr = [["a",12],["b",15],["c",10],["d",19],["e",20]]
let resArr = []
let resObj = {}
arr.map(( ele,index ) => {
    resObj.label = ele[0]
    resObj.value = ele[1]
    resArr[index] = resObj
})

and I am getting output as -

resObj = 
[{label:"e",value:12},
{label:"e",value:12},
{label:"e",value:12},
{label:"e",value:12},
{label:"e",value:12}]

The last array content is copied to whole object.

Output should be -

resObj = 
[{label:"a",value:12},
{label:"b",value:15},
{label:"c",value:10},
{label:"d",value:19},
{label:"e",value:20}]

Please help me. Thanks in Advance. Please mention what is the mistake in my code

Please comment, how should I edit this question to get it up voted.

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

0

You should create resObj inside the map callback; otherwise, you are modifying the same object in each iteration. Additionally, you should directly return resObj in the callback and use the result of map as the result array, as Array#map returns an array created by the return values of the callback after being called on each element of the original array.

let arr = [["a",12],["b",15],["c",10],["d",19],["e",20]]
let resArr = arr.map(( ele,index ) => {
    let resObj = {}
    resObj.label = ele[0]
    resObj.value = ele[1]
    return resObj;
});
console.log(resArr);

For a simpler solution, you can use destructuring and shorthand property names.

let arr = [["a",12],["b",15],["c",10],["d",19],["e",20]]
let resArr = arr.map(([label, value]) => ({label, value}));
console.log(resArr);

about 4 years ago · Juan Pablo Isaza Relatório

0

The issue is caused by referring to and changing the same object over and over again. You should create the object inside your map() to avoid that. By the way you can use a shorter version like this:

    let arr = [
      ["a", 12],
      ["b", 15],
      ["c", 10],
      ["d", 19],
      ["e", 20]
    ];
    let resArr = arr.map(ele => {
      return {
        label: ele[0],
        value: ele[1]
      }
    });
    console.log(resArr);

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