javascript - 尝试将对象数组转换为 JavaScript 对象

标签 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',
    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/

相关文章:

java - 如何生成随机列表并插入到listview中

Java 从主函数调用打印方法并使用来自另一个单独方法的数据

java - 在Java中打印数组

javascript - 如何测试 RxJS 事件

javascript - Nodejs 中长时间运行的请求

javascript - Uncaught ReferenceError : Unable to process binding with Ajax

java - 奇怪的 if 语句行为

javascript - 谷歌地图 api 不显示

javascript - 如何在 vert.x 中运行 node.js 应用程序?

javascript - 循环遍历对象数组并在 JSON 中添加对象 - Javascript