我对餐馆的订单有以下异议。

   var obj = {
      orders: [
        null,
        {
          date: "2018-07-09 10:07:18",
          orderVerified : true,
          item: [
            {
              name: "apple juice",
              price: 3.9,
              quantity: 1,
              isDrink: true
            },
            {
              name: "Hawaii pizza",
              price: 7,
              quantity: 2,
              isDrink: false
            }
          ]
        },
        {
          date: "2018-07-09 10:07:30",
          orderVerified : false,
          item: [
            {
              name: "Warmer Topfenstrudel",
              price: 3.9,
              quantity: 1,
              isDrink: false
            }
          ]
        },
        {
          date: "2018-07-09 15:07:18",
          orderVerified : true,
          item: [
            {
              name: "Coca Cola 2 l",
              price: 12.9,
              quantity: 3,
              isDrink:true
            }
          ]
        },
        {
          date: "2018-06-13 10:07:18",
          orderVerified : true,
          item: [
            {
              name: "Wiener Schnitzel vom Schwein",
              price: 9.9,
              quantity: 2,
              isDrink: false
            }
          ]
        }
      ]
    };

如果isdrink的值是真的,我想把这个项目的价格*项目数量加起来。
我试过使用这个函数,它得到所有项目的总数。但是我可以检查一下Isdrink是真是假,但是我怎么能把饮料的价格和数量加起来呢?
 fullTotal: function(arr) {
    if (arr!=''){
    return arr.reduce((sum, order) => {
      return sum + order.item.reduce((itemSum, item) => (
        itemSum + (item.price * item.quantity)
      ), 0)
    },0)}
    else {return 0}
  },

请给我一些建议。谢谢您!

最佳答案

你必须:
从案例中的null值筛选数组
从非饮料中筛选项目[属性isDrink]

var obj = {
  orders: [
    null,
    {
      date: "2018-07-09 10:07:18",
      orderVerified: true,
      item: [{
          name: "apple juice",
          price: 3.9,
          quantity: 1,
          isDrink: true
        },
        {
          name: "Hawaii pizza",
          price: 7,
          quantity: 2,
          isDrink: false
        }
      ]
    },
    {
      date: "2018-07-09 10:07:30",
      orderVerified: false,
      item: [{
        name: "Warmer Topfenstrudel",
        price: 3.9,
        quantity: 1,
        isDrink: false
      }]
    },
    {
      date: "2018-07-09 15:07:18",
      orderVerified: true,
      item: [{
        name: "Coca Cola 2 l",
        price: 12.9,
        quantity: 3,
        isDrink: true
      }]
    },
    {
      date: "2018-06-13 10:07:18",
      orderVerified: true,
      item: [{
        name: "Wiener Schnitzel vom Schwein",
        price: 9.9,
        quantity: 2,
        isDrink: false
      }]
    }
  ]
};

function fullTotal(arr) {
  if (arr != '') {
    return arr.filter(order => order != null).reduce((sum, order) => {
      return sum + order.item.filter(item => item.isDrink).reduce((itemSum, item) => (
        itemSum + (item.price * item.quantity)
      ), 0)
    }, 0)
  } else {
    return 0
  }
}

console.log(fullTotal(obj.orders));

09-25 20:47