anesongib
anesongib

Reputation: 37

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?

Upvotes: 3

Views: 108

Answers (4)

XMehdi01
XMehdi01

Reputation: 1

This code extracts x and y values from an array of pairs and organizes them into an object with separate arrays for x values (keys) and y values (values).

const pairs = [["x1", "y1"], ["x2", "y2"], ["x3", "y3"]]

const keys = pairs.map(([x, _]) => x); 
const values = pairs.map(([_, y]) => y);

const result = { keys, values };

console.log(result);

Upvotes: 0

Ori Drori
Ori Drori

Reputation: 192262

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);

Upvotes: 5

pilchard
pilchard

Reputation: 12918

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());

Upvotes: 3

Reflective
Reflective

Reputation: 3917

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);

Upvotes: 1

Related Questions