2

Working with an array of objects like:

const objArr = [
  { k: 'row 1', v: '111' },
  { k: 'row 2', v: '222' },
  { k: 'row 3', v: null },
  { k: 'row 4', v: null },
];

What is the most efficient way to reduce into a single object using the value of k for the key and the value of v for the value?

Desired Outcome:

const obj = {
  'row 1': '111',
  'row 2': '222',
  'row 3': null,
  'row 4': null,
};

I have attempted to accomplish with a one-liner like:

const obj = objArr.reduce((obj, key) => ({ ...{ [key]: objArr.v[key] } }), {});
proph3t
  • 865
  • 2
  • 7
  • 25

0 Answers0