Conversion of array of object to object

Viewed 74

I have an array of objects:

const action_triggers = [
  {
    onClick: {
      action_id: "1",
    },
  },
  {
    onLoad: {
      action_id: "2",
    },
  },
];

How do I convert it into the following by JavaScript?

const action_trigger = {
  onClick: {
    action_id: 1
  },
  onLoad: {
    action_id: 2
  }
}
1 Answers

Here is the reduce version

const _l = (s) => {
    console.log(s);
};
let flatter = action_triggers.reduce((propogator, prop) => {
    for (const [key, value] of Object.entries(prop)) {
        propogator[key] = value;
    }
    return propogator;
}, {});
_l(flatter);

Helps with conditionally doing whatever you want to {} the input

Related