0

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 !!

10
  • How does {"10": 1, "20": 3, "30": 1, "31": 1} relate to the array above it? Commented Mar 29, 2018 at 10:29
  • what condition apply to convert from the first array to the object ? Commented Mar 29, 2018 at 10:29
  • We should guess the error? Or maybe you will be kind and post error's text? Commented Mar 29, 2018 at 10:29
  • @DominicTobias numbers are values of NotificationItems. Commented Mar 29, 2018 at 10:30
  • I am not getting error but I am not getting correct output also Commented Mar 29, 2018 at 10:30

1 Answer 1

3

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.