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

时间:2018-03-29 10:27:40

标签: 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);
}

&#13;
&#13;
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);
}
&#13;
&#13;
&#13;

我是JavaScript的新手,我成功获取了我必须转换为JavaScript对象的值,但我得到了正确的输出。我评论的那些行没有给我正确的结果。请给出一些提示。在此先感谢!!

1 个答案:

答案 0 :(得分:3)

这应该使用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);