Building a JSON object from an array – Typescript

I have an array like this:

[
  {
    "fact": "field1",
    "operator": "equal",
    "value": "field1_value"
  },
  {
    "fact": "field2",
    "operator": "equal",
    "value": "field2_value"
  },
  {
    "fact": "field3",
    "operator": "equal",
    "value": "field3_value"
  },
  {
    "fact": "field4",
    "operator": "equal",
    "value": "field4_value"
  },
  {
    "fact": "field5",
    "operator": "equal",
    "value": "field5_value"
  }
]

I want to build an object like this:

{
  field1: field1_value,
  field2: field2_value,
  field3: field3_value,
  field4: field4_value,
  field5: field5_value,
}

I have thousands of objects in the source array and I have thousands of such arrays to work with. What’s the best way to do this in typescript?

68 thoughts on “Building a JSON object from an array – Typescript”