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

139
Visualizações
Convert 2D array to array of objects

I have been trying to convert this data:

var myArray = [
[1,2,3,4],
["1","2","3","4"],
["a", "b", "c", "d"]
]

to this:

var result = [
  {
    "num": 1,
    "str": "1",
    "let": "a"
  },
  {
    "num": 2,
    "str": "2",
    "let": "b"
  },
  {
    "num": 3,
    "str": "3",
    "let": "c"
  },
  {
    "num": 4,
    "str": "4",
    "let": "d"
  }
]

Any assistance will be extremely helpful. I have tried various iterations but none of them yield what I am hoping for.

I am trying to use this:

objectified_data = array_data.map(function(x) {
    return {    
        "num": x[0],
        "str": x[1],
        "let": x[2]
    }
})

But I cannot figure out how to make the homogenous set of array, heterogenous for this to work. Ultimately, I am trying to filter out certain keys, so the original data as it was is not working for that task

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

0

You can achieve this with a simple for

var myArray = [
  [1, 2, 3, 4],
  ["1", "2", "3", "4"],
  ["a", "b", "c", "d"]
]
const json = [];

for (let i = 0; i < myArray[0].length; i++) {
  json.push({
    num: myArray[0][i],
    let: myArray[1][i],
    str: myArray[2][i],
  });
}

console.log(json);

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this

var myArray = [[1,2,3,4],["1","2","3","4"],["a", "b", "c", "d"]];

let result = myArray[0].map((v, i) => ({
    "num": myArray[0][i],
    "let": myArray[1][i],
    "str": myArray[2][i]
}))

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

Just a basic solution. You would want i to be dynamic I believe ? In that case you can just check the first nested array's size and use that as i.

    let x = 0;
    let y = 1;
    let z = 2;
    let json = [];
    for(let i=0; i<4;i++){
        let item = {};
        item = {
            ["num"]: myArray[x][i],
            ["let"]: myArray[y][i],
            ["str"]: myArray[z][i]
        }
        result.push(item);
    }
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