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": 1, "20": 3, "30": 1, "31": 1}relate to the array above it?NotificationItems.