JSFiddle - React, Tailwind, and code Playground

by ttquang1063750

JavaScript

const input = [
  {text1: 'John', text2: 'male', may: 'alo'},
  {text1: 'Philip', text2: 'male'},
  {text1: 'Matthew', text2: 'male'},
  {text1: 'Richard', text2: 'male'},
];

function toKeyArray(array) {
  const obj = {};
  array.forEach(item => {
    const keys = Object.keys(item);
    for (const k of keys) {
      (obj[k] = obj[k] || []).push(item[k]);
    }
  });

  return obj;
}

const output = toKeyArray(input);
console.log(output);