1

Given an array of two-entry tuples, for example:

const arr = [
    ['key1', 'value1'],
    ['key2', 'value2'],
    ['key3', 'value3'],
    ['key2', 'value4'],
    ['key3', 'value5'],
    ['key3', 'value6']
];

I'm looking for neat way to get:

const obj = {
    key1: ['value1'],
    key2: ['value2', 'value4'],
    key3: ['value3', 'value5', 'value6']
};

So far I've got:

const obj = Object.assign({}, ...arr.map(x => ({[x[0]]: x[1]})));

Which gives me:

const obj = {
    key1: 'value1',
    key2: 'value4',
    key3: 'value6'
};

I feel that I can somehow use reduce in order to obtain the desired output.

I am using NodeJS (not a browser).

4 Answers 4

3

You could use Map Object.

const arr = [
  ['key1', 'value1'],
  ['key2', 'value2'],
  ['key3', 'value3'],
  ['key2', 'value4'],
  ['key3', 'value5'],
  ['key3', 'value6'],
];

const map = new Map();
arr.forEach(([x, y]) => map.set(x, map.has(x) ? [...map.get(x), y] : [y]));
const ret = Object.fromEntries(map.entries());
console.log(ret);

Sign up to request clarification or add additional context in comments.

Comments

1

Yes you could do this with reduce method where you use an object as a accumulator value.

const arr = [["key1","value1"],["key2","value2"],["key3","value3"],["key2","value4"],["key3","value5"],["key3","value6"]]

const result = arr.reduce((r, [k, v]) => {
  if (!r[k]) r[k] = [v]
  else r[k].push(v)
  return r
}, {})

console.log(result)

Comments

1

Reduce is a perfect use case here

const arr = [
    ['key1', 'value1'],
    ['key2', 'value2'],
    ['key3', 'value3'],
    ['key2', 'value4'],
    ['key3', 'value5'],
    ['key3', 'value6']
];

const res = arr.reduce((acc, [key, value]) => {
  // Create the key:value and append new value
  acc[key] = acc[key] || [];
  acc[key] = [...acc[key], value];
  return acc;
}, {});

console.log(res)

2 Comments

So const res = arr.reduce((acc, [key, value]) => ({[...(acc[key] || []), value]}), {});?
I would avoid nonreadable code. It is might be cool to reduce 6 lines of code to 1, but it is not maintainable. :)
0

Here's how I'm using this in my React/TS project to create select options!

export const mapToSelectOptionsFormTupleOfTwo = (
    tupleArray: ["string", "string"][]
) => {
    const selectOptions = new Array();
    tupleArray?.forEach(([x, y]) =>
        selectOptions.push({ value: lowercaseFirstLetter(x), label: y })
    );

    return selectOptions;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.