Vikas
Vikas

Reputation: 985

Trying to convert an array of objects to JavaScript Object

I am trying to convert the given array

[ { slug: 'invoice',
    display: 'Invoice',
    channels: { sms: true, email: false } },
  { slug: 'payment',
    display: 'Payment',
    channels: { sms: true, email: true } },
  { slug: 'manual_reminder',
    display: 'Manual Reminder',
    channels: { sms: true, email: false } },
  { slug: 'automatic_reminder',
    display: 'Automatic Reminder',
    channels: { sms: true, email: false } } ]

into this array

{"10": 1, "20": 3, "30": 1, "31": 1}

I have written the code for converting the above array to this array but still I am not getting correct output

const NotificationChannels = {
  SMS: 1, // 01
  EMAIL: 2 // 10
  // BOTH: 3
};

const NotificationItems = {
  INVOICE: 10,
  PAYMENT: 20,
  MANUAL_REMINDER: 30,
  AUTOMATIC_REMINDER: 31
};

const getVal = channels => {
  if (channels.sms === true && channels.email === false)
    return NotificationChannels.SMS;

  if (channels.sms === false && channels.email === true)
    return NotificationChannels.EMAIL;

  if (channels.sms === true && channels.email === true)
    return NotificationChannels.EMAIL | NotificationChannels.SMS;
};

let arr = req.body.notification_settings;
console.log(arr);
for (let i = 0; i < arr.length; i++) {
  console.log(arr[i].channels);

  console.log(getVal(arr[i].channels));

  var x = NotificationItems[arr[i].slug.toString().toUpperCase()];
  console.log(x);

  // databaseObject.x = getVal(arr[i].channels);

  // console.log(arr[i].slug.toString().toUpperCase());
  // console.log(arr[i].channels);
}

const req = {
	body: {
  	notification_settings: [
    	{ slug: 'invoice', display: 'Invoice', channels: { sms: true, email: false } },
    	{ slug: 'payment', display: 'Payment', channels: { sms: true, email: true } },
    	{ slug: 'manual_reminder', display: 'Manual Reminder', channels: { sms: true, email: false } },
    	{ slug: 'automatic_reminder', display: 'Automatic Reminder', channels: { sms: true, email: false } }
  	]
  }
};

const NotificationChannels = {
  SMS: 1, // 01
  EMAIL: 2 // 10
  // BOTH: 3
};

const NotificationItems = {
  INVOICE: 10,
  PAYMENT: 20,
  MANUAL_REMINDER: 30,
  AUTOMATIC_REMINDER: 31
};

const getVal = channels => {
  if (channels.sms === true && channels.email === false)
    return NotificationChannels.SMS;

  if (channels.sms === false && channels.email === true)
    return NotificationChannels.EMAIL;

  if (channels.sms === true && channels.email === true)
    return NotificationChannels.EMAIL | NotificationChannels.SMS;
};


let arr = req.body.notification_settings;
for (let i = 0; i < arr.length; i++) {
  console.log(arr[i].channels);

  console.log(getVal(arr[i].channels));

  var x = NotificationItems[arr[i].slug.toString().toUpperCase()];
  console.log(x);
}

I am new to JavaScript and I am successful in getting values which I have to convert to the JavaScript Object but I am getting correct output . The lines which I have commented did not gave me correct result . Please give some hint . Thanks in advance !!

Upvotes: 0

Views: 62

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97130

This should do it, using Array.prototype.reduce():

const result = data.reduce((a, v) => {
  a[NotificationItems[v.slug.toUpperCase()]] = (v.channels.sms ? 1 : 0) +
                                               (v.channels.email ? 2 : 0);
  return a;
}, {});

Or to make it completely dynamic:

const result = data.reduce((a, v) => {
  a[NotificationItems[v.slug.toUpperCase()]] = Object.keys(v.channels).reduce((b, w) => b + (v.channels[w] ? NotificationChannels[w.toUpperCase()] : 0), 0)
  return a;
}, {});

Full snippet:

const NotificationChannels = {
  SMS: 1, // 01
  EMAIL: 2 // 10
  // BOTH: 3
};

const NotificationItems = {
  INVOICE: 10,
  PAYMENT: 20,
  MANUAL_REMINDER: 30,
  AUTOMATIC_REMINDER: 31
};

const data = [{
    slug: 'invoice',
    display: 'Invoice',
    channels: {
      sms: true,
      email: false
    }
  },
  {
    slug: 'payment',
    display: 'Payment',
    channels: {
      sms: true,
      email: true
    }
  },
  {
    slug: 'manual_reminder',
    display: 'Manual Reminder',
    channels: {
      sms: true,
      email: false
    }
  },
  {
    slug: 'automatic_reminder',
    display: 'Automatic Reminder',
    channels: {
      sms: true,
      email: false
    }
  }
];

const result = data.reduce((a, v) => {
  a[NotificationItems[v.slug.toUpperCase()]] = Object.keys(v.channels).reduce((b, w) => b + (v.channels[w] ? NotificationChannels[w.toUpperCase()] : 0), 0)
  return a;
}, {});

console.log(result);

Upvotes: 3

Related Questions