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

312
Visualizações
How to assign Array of values as keys to another array of values?

What is the Best way to convert,

{
   "columns":[
      "name",
      "color"
   ],
   "values":[
      [
         "lion",
         "yellow"
      ],
      [
         "crow",
         "black"
      ]
   ]
}

into

{
   "data":[
      {
        "name":"lion",
        "color":"yellow"
      },
      {
         "name":"crow",
         "color":"black"
      }
   ]
}

Rather than loop, Is there any function available ? Can I achieved it through something like extend() ?

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

0

You could use Object.assign with spread syntax ... for the parts.

var object = { columns: ["name", "color"], values: [["lion", "yellow"], ["crow", "black"]] },
    result = { data: object.values.map(v => Object.assign(...object.columns.map((c, i) => ({[c]: v[i]})))) };

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

over 4 years ago · Santiago Trujillo Relatório

0

You can do do it with combination of map and reduce. Reducing original columns array will allow to code to work with any number of columns, it will pick up corresponding value by index:

const result = {data: data.values.map(el => {
  return data.columns.reduce((prev, curr, index) => {
    prev[curr] = el[index]
    return prev
  }, {})
})}

Check the demo below.

const data = {
   "columns":[
      "name",
      "color"
   ],
   "values":[
      [
         "lion",
         "yellow"
      ],
      [
         "crow",
         "black"
      ]
   ]
}

const result = {data: data.values.map(el => {
  return data.columns.reduce((prev, curr, index) => {
    prev[curr] = el[index]
    return prev
  }, {})
})}

console.log(result)

over 4 years ago · Santiago Trujillo Relatório

0

You can get vales of .columns array using destructuring assignment, spread element; for..of loop to assign computed properties of .columns array as strings as properties of objects at data array by iterating .values property of original object, assign value of each array as value of created object

let obj = {
   "columns":[
      "name",
      "color"
   ],
   "values":[
      [
         "lion",
         "yellow"
      ],
      [
         "crow",
         "black"
      ]
   ]
}

let [res, key, value] = [{data:Array()}, ...obj.columns];

for (let [a, b] of [...obj.values]) res.data.push({[key]:a, [value]:b});

console.log(res);

over 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