我正在尝试转换给定的数组
[ { 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 } } ]
进入这个数组
{"10": 1, "20": 3, "30": 1, "31": 1}
我已经编写了将上面的数组转换为这个数组的代码 但我仍然没有得到正确的输出
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);
}
我是 JavaScript 新手,我成功获取了必须转换为 JavaScript 对象的值,但我得到了正确的输出。我评论的行没有给我正确的结果。请给一些提示。预先感谢!
最佳答案
这应该可以做到,使用 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;
}, {});
或者使其完全动态:
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;
}, {});
完整片段:
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);
关于javascript - 尝试将对象数组转换为 JavaScript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49553739/