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

104
Visualizações
how to make object's array from several array

For example,

I have a total of two arrangements.

The key array has keys.

The remaining arrays are multi arrays in one array like [[A], [B], [C], ...]

So how do we put objects in one array?

let key = [“name”, “age”, “country”];

let values = [["james", "23", "US"],["steve", "20", "UK"], ...]


** expect result **
result = [
    {
        "name": "james",
        "age": 23,
        "country": "US"
    },
    {
        "name": "steve",
        "age": 20,
        "country": "UK"
    },
    ...
]


about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

if you have data as name age and country with same keys so you can try below code it should work for you.

let values = [["james", "23", "US"],["steve", "20", "UK"], ...]
const result = [];

values.forEach(item => {
    const [name, age, country] = item;
    result.push({ name, age, country });
});

console.log(result)
about 4 years ago · Santiago Trujillo Relatório

0

Single line using array map and reduce functions:

values.map(v=>(key.reduce((acc, k, i) => ({...acc, [k]: v[i]}), {})));

(Note your original question contains non-ASCII quotation marks “” - you need to use " for valid JS syntax).

about 4 years ago · Santiago Trujillo Relatório

0

I just asked your question to OpenAI JavaScript Codex and it returned this result.

/* let key = [“name”, “age”, “country”]; */
var key = ["name", "age", "country"];
/* let values = [["james", "23", "US"],["steve", "20", "UK"]] */
var values = [["james", "23", "US"],["steve", "20", "UK"]];
/* So how do we put objects in one array? */
var objects = [];
for (var i = 0; i < values.length; i++) {
  var object = {};
  for (var j = 0; j < key.length; j++) {
    object[key[j]] = values[i][j];
  }
  objects.push(object);
}

I really don't know if it works or not, but I am curious to see what it does. You should try this.

Please forgive me if it doesn't work. Thanks

about 4 years ago · Santiago Trujillo 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