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

117
Visualizações
Converting Array of Pairs into 2 Separate Arrays Without Iteration

I have an array of pairs that looks like this [[x,y], [x,y] ... ]. I want to format it into an Object where the values are arrays of x and y values like so {keys: [x1, x2, x3 ...], values: [y1, y2, y3 ... ]}.

Are there any array/object operations to complete this operation without iterating through the original list?

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

0

The easiest and safest way is to reduce the array to an object, although it requires a loop:

const input = [[3, 300], [2, 200], [1, 100]];

const result = input.reduce((acc, [key, val]) => {
  acc.keys.push(key);
  acc.values.push(val);
  
  return acc;
}, { keys: [], values: [] });

console.log(result);

I wouldn't actually use the convert to object / Map method (under Original Answer), because it has a serious caveat - duplicate entries the has the same keys would be overridden.

For example:

const input = [[3, 300], [3, 200], [3, 100]];

const obj = Object.fromEntries(input);
const result = { keys: Object.keys(obj), values: Object.values(obj) };

console.log(result);

Original Answer

Building on top of pilchard's answer, I would convert the array to a Map, and then take the Map's keys, and values. I would use a Map, and not an object, because object's keys are always strings, and if the values are integers, the object would also be sorted by their value. A Map would preserve the original type, and won't reorder them.

const input = [[3, 300], [4, 200], [1, 100]];

const map = new Map(input);
const result = { keys: [...map.keys()], values: [...map.values()] };

console.log(result);

An example of converting the same structure to an object:

const input = [[3, 300], [4, 200], [1, 100]];

const obj = Object.fromEntries(input);
const result = { keys: Object.keys(obj), values: Object.values(obj) };

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

The process is iterative, but you can hide it by using existing Object methods: Object.fromEntries(), Object.keys(), Object.values()

const input = [['a', 1], ['b', 2], ['c', 3]];

const obj = Object.fromEntries(input);
const result = { keys: Object.keys(obj), values: Object.values(obj) };

console.log(result);

Ori Drori's refinement using a Map is more robust not only for numeric values but for any type.

const input = [[new Date(), { y: 1 }], [new Date(), { y: 2 }], [new Date(), { y: 3 }]];

const map = new Map(input);
const result = { keys: [...map.keys()], values: [...map.values()] };

console.log(result);
console.log(result.keys[0].valueOf());

about 4 years ago · Juan Pablo Isaza Relatório

0

No space left for losers, but just another way

const input = [[3, 300], [4, 200], [1, 100]];
const result = { keys: Array.from(input, x => x[0]), values: Array.from(input, x => x[1]) };
console.log(result);

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