[Solved] Turn json object value become a key and value in javascript [closed]


You can use Object.fromEntries():

const obj = [
  {
      "configSlug": "receiptStoreName",
      "configContent": "The Store Name"
  },
  {
      "configSlug": "receiptStoreAddress",
      "configContent": "Cattle Street"
  },
  {
      "configSlug": "receiptStorePhone",
      "configContent": "01 123234"
  },
  {
      "configSlug": "receiptStoreFoot1",
      "configContent": "Thanks For Visiting"
  }
];

const result = Object.fromEntries(obj.map(entry => [entry.configSlug, entry.configContent]));

console.log(result);

Or you can use a simple loop:

const obj = [
  {
      "configSlug": "receiptStoreName",
      "configContent": "The Store Name"
  },
  {
      "configSlug": "receiptStoreAddress",
      "configContent": "Cattle Street"
  },
  {
      "configSlug": "receiptStorePhone",
      "configContent": "01 123234"
  },
  {
      "configSlug": "receiptStoreFoot1",
      "configContent": "Thanks For Visiting"
  }
];

const result = {};
for (const entry of obj) {
  result[entry.configSlug] = entry.configContent;
}

console.log(result);

solved Turn json object value become a key and value in javascript [closed]