How do I convert an array of Objects into one Object using code

I have an array of phone numbers:

"PhoneNumbers":[
   {
      "Number":"(123) 444-5555",
      "Type":"Home"
   },
   {
      "Number":"(234) 555-6666",
      "Type":"Office"
   },
   {
      "Number":"(345) 666-7777",
      "Type":"Fax"
   },
   {
      "Number":"(456) 777-8888",
      "Type":"Cell"
   }
]

I need it to look like this:

"PhoneNumbers":{
      "Home":"(123) 444-5555",
      "Office":"(234) 555-6666",
      "Fax":"(345) 666-7777",
      "Cell":"(456) 777-8888"
   }

Any ideas of how to accomplish this with code?

Welcome to the community @ULX!

You can use the following code in a Code-Node:

for (const item of $input.all()) {
  item.json.PhoneNumbers = item.json.PhoneNumbers.reduce((acc, cur) => {
    acc[cur.Type] = cur.Number;
    return acc;
  }, {})
}

return $input.all();

Here an example workflow:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.