尝试将对象数组转换为JavaScript对象
我正在尝试转换给定的数组尝试将对象数组转换为JavaScript对象,javascript,arrays,Javascript,Arrays,我正在尝试转换给定的数组 [ { slug: 'invoice', display: 'Invoice', channels: { sms: true, email: false } }, { slug: 'payment', display: 'Payment', channels: { sms: true, email: true } }, { slug: 'manual_reminder', display: 'Manual Reminder
[ { 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 NotificationChannels={
短信:1,//01
电邮:2//10
//两者:3
};
施工通知项目={
发票:10,
付款:20,
手动提醒:30,
自动提醒:31
};
const getVal=channels=>{
如果(channels.sms==true&&channels.email==false)
返回NotificationChannels.SMS;
如果(channels.sms==false&&channels.email==true)
返回NotificationChannels.EMAIL;
如果(channels.sms==true&&channels.email==true)
返回NotificationChannels.EMAIL | NotificationChannels.SMS;
};
设arr=req.body.notification_设置;
控制台日志(arr);
for(设i=0;i
const-req={
正文:{
通知\u设置:[
{slug:'invoice',display:'invoice',频道:{sms:true,email:false},
{slug:'payment',display:'payment',频道:{sms:true,email:true},
{slug:'手动提醒',显示:'手动提醒',频道:{sms:true,email:false},
{slug:'自动提醒',显示:'自动提醒',频道:{sms:true,email:false}
]
}
};
const NotificationChannels={
短信:1,//01
电邮:2//10
//两者:3
};
施工通知项目={
发票:10,
付款:20,
手动提醒:30,
自动提醒:31
};
const getVal=channels=>{
如果(channels.sms==true&&channels.email==false)
返回NotificationChannels.SMS;
如果(channels.sms==false&&channels.email==true)
返回NotificationChannels.EMAIL;
如果(channels.sms==true&&channels.email==true)
返回NotificationChannels.EMAIL | NotificationChannels.SMS;
};
设arr=req.body.notification_设置;
for(设i=0;i
这应该通过以下方式完成:
或者使其完全动态:
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={
短信:1,//01
电邮:2//10
//两者:3
};
施工通知项目={
发票:10,
付款:20,
手动提醒:30,
自动提醒:31
};
常数数据=[{
slug:'发票',
显示:“发票”,
频道:{
短信:是的,
电子邮件:false
}
},
{
slug:“付款”,
显示:“付款”,
频道:{
短信:是的,
电子邮件:真的
}
},
{
slug:“手动提醒”,
显示:“手动提醒”,
频道:{
短信:是的,
电子邮件:false
}
},
{
slug:“自动提醒”,
显示:“自动提醒”,
频道:{
短信:是的,
电子邮件:false
}
}
];
常数结果=数据减少((a,v)=>{
a[NotificationItems[v.slug.toUpperCase()]=Object.keys(v.channels).reduce((b,w)=>b+(v.channels[w]?NotificationChannels[w.toUpperCase()]:0),0)
返回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={
短信:1,//01
电邮:2//10
//两者:3
};
施工通知项目={
发票:10,
付款:20,
手动提醒:30,
自动提醒:31
};
常数数据=[{
slug:'发票',
显示:“发票”,
频道:{
短信:是的,
电子邮件:false
}
},
{
slug:“付款”,
显示:“付款”,
频道:{
短信:是的,
电子邮件:真的
}
},
{
slug:“手动提醒”,
显示:“手动提醒”,
频道:{
短信:是的,
电子邮件:false
}
},
{
slug:“自动提醒”,
显示:“自动提醒”,
频道:{
短信:是的,
电子邮件:false
}
}
];
常数结果=数据减少((a,v)=>{
a[NotificationItems[v.slug.toUpperCase()]=Object.keys(v.channels).reduce((b,w)=>b+(v.channels[w]?NotificationChannels[w.toUpperCase()]:0),0)
返回a;
}, {});
控制台日志(结果)
如何将{“10:1”,20:3,30:1,31:1}
与上面的数组关联?应用什么条件将第一个数组转换为对象?我们应该猜测错误?或者你会很友善地发布错误的文本?@DominicTobias数字是通知项的值
。我没有收到错误,但我也没有收到正确的输出。{“10:1”,20:3,30:1,31:1}
与上面的数组相关?从第一个数组转换为对象需要什么条件?我们应该猜测错误?或者你会很友善地发布错误的文本?@DominicTobias数字是通知项的值
。我没有收到错误,但也没有收到正确的输出