【问题标题】:Count value inside object inside object对象内部的计数值
【发布时间】:2017-12-01 19:05:38
【问题描述】:

我在一个对象中有一个对象,我想计算数量并返回值。

例子:

cart: {items: {A {number: 'xxx', quantity: 1, price: 999}, B {number: 'xxx', quantity: 3, price: 999}, C {number: 'xxx', quantity: 2, price: 999} }}

我现在想要的是数量计数 6 的返回。

这是我迄今为止所取得的成就。我该如何进行?

function() {
  var value = {{DataWithInformations}}
  var itemCount = cart.items.quantity
}

非常感谢。

【问题讨论】:

    标签: javascript object javascript-objects


    【解决方案1】:

    有几种方法可以解决。

    最干净的方法之一可能是使用reduce 函数:

    data.cart.items.reduce((sum, item) => sum + item.quantity, 0);
    

    用 ES5 编写(没有箭头函数),它看起来像这样:

    data.cart.items.reduce(function (sum, item) { return sum + item.quantity }, 0);
    

    (注意:您发布的不是有效的 JSON 或有效的对象,所以我对对象的实际情况做了一些猜测。

    const data = {
      cart: {
        items: [
          { quantity: 1 },
          { quantity: 2 },
          { quantity: 3 }
        ]
      }
    };
    
    const sum = data.cart.items.reduce((sum, item) => sum + item.quantity, 0);
    console.log(sum);

    更新到问题中更改的对象:

    ES6:

    Object.keys(a.cart.items).reduce((sum, key) => sum + a.cart.items[key].quantity, 0);
    

    ES5:

    Object.keys(a.cart.items).reduce(function (sum, key) { return sum + a.cart.items[key].quantity, 0);
    

    const a = {
      cart: {
        items: {
          A: {number: 'xxx', quantity: 1, price: 999}, 
          B: {number: 'xxx', quantity: 3, price: 999}, 
          C :{number: 'xxx', quantity: 2, price: 999} 
        }
      }
    };
    
    const result = Object.keys(a.cart.items).reduce((sum, key) => sum + a.cart.items[key].quantity, 0);
    console.log(result);

    【讨论】:

    • 不是我看到的,虽然它不适用于您刚刚发布的更新对象。请稍等,我会更新代码示例。
    • 哦,猜猜更新不是你的,但其他人对你的帖子有不同的解释。他们可能是正确的,而不是我的假设。
    • data.cart.items.reduce(function (sum, item) { return sum + item.quantity }, 0); 正是我需要的,谢谢。
    【解决方案2】:

    您可以使用 reduce 函数从数组中获取值。 PS:您提供的数据结构在语法上不正确,所以我将做一些假设。

    var data = {
      cart: {
        items: [{
            number: 'xxx',
            quantity: 1,
            price: 999
          },
          {
            number: 'xxx',
            quantity: 3,
            price: 999
          },
          {
            number: 'xxx',
            quantity: 2,
            price: 999
          }
        ]
    
      }
    };
    
    var qtySum = data.cart.items.reduce(function(mem, item) {
      mem += item.quantity;
      return mem;
    }, 0);
    
    console.log("Total count:", qtySum);

    【讨论】:

      【解决方案3】:

      您需要循环到 JSON,然后添加 quantity。

      var jsonData = {'cart':
                          {'items': 
                                      {
                      'A':{'number': 'xxx', 'quantity': 1,'price': 999}, 
                      'B' : {'number': 'xxx', 'quantity': 3, 'price': 999},
                      'C':{'number': 'xxx', 'quantity': 2, 'price': 999}
                      }
                }
              };
      var keys = Object.keys(jsonData.cart.items);
      
      var quantity = 0;
      for(var i=0; i<keys.length; i++){
         var key = keys[i];
         var item = jsonData.cart.items[key];
         quantity += item.quantity;
      }
      
      alert(quantity);
      

      这里是JSFIDDLE的链接

      【讨论】:

        【解决方案4】:

        这应该是一个解决方案:

        a = {cart:{items:{A:{number: 'xxx', quantity: 1, price: 999}, B: {number: 'xxx', quantity: 3, price: 999}, C :{number: 'xxx', quantity: 2, price: 999} }}};
        
        sum=0;
        for(key in a.cart.items){
          sum += a.cart.items[key].quantity;
        }
        console.log(sum);

        【讨论】:

          【解决方案5】:

          我会使用for 循环来解决您的问题。 我看到你在cart 后面加了一个冒号,所以我猜你的购物车在另一个 JSON 对象中。

          var foo = {
            cart: {
              items: {
                A: {number: 'xxx', quantity: 1, price: 999},
                B: {number: 'xxx', quantity: 3, price: 999},
                C: {number: 'xxx', quantity: 2, price: 999}
              }
            }
          };
          
          // This is what you need
          document.getElementById("please-click-me").onclick = function() {
            var sum = 0;
            // Note that the 'in' gives you the keys, not the objects themselves
            for (var key in foo.cart.items) {
              sum += foo.cart.items[key].quantity;
            }
            alert(sum);
          }
          &lt;button id="please-click-me"&gt;Click&lt;/button&gt;

          希望这会有所帮助:)。

          【讨论】:

            【解决方案6】:

            一个干净的 ES6 解决方案:

            const cart = {
              items: {
                A: {number: 'xxx', quantity: 1, price: 999}, 
                B: {number: 'xxx', quantity: 3, price: 999}, 
                C :{number: 'xxx', quantity: 2, price: 999} 
              }
            };
            
            const total = Object.values(cart.items)
                                .map((obj) => obj.quantity)
                                .reduce((prev, curr) => prev + curr, 0);
            
            console.log(total);

            【讨论】:

              猜你喜欢
              • 2019-11-10
              • 1970-01-01
              • 1970-01-01
              • 2019-06-06
              • 2018-07-30
              • 2015-05-04
              • 2020-10-06
              • 1970-01-01
              • 2022-11-03
              相关资源
              最近更新 更多