【问题标题】:Sum up array with objects用对象求和数组
【发布时间】:2018-06-03 19:28:03
【问题描述】:

我有以下包含对象的数组,我想总结所有出现的watt

let allProducts = [{
    "unique_id": "102",
    "currency": "$",
    "price": "529.99",
    "watt": 150
  },
  {
    "unique_id": "11",
    "currency": "$",
    "price": "323",
    "watt": 150
  },
  {
    "unique_id": "13",
    "currency": "$",
    "price": "23",
    "watt": 77
  }
]

let getWatt =
  _(allProducts)
  .map((objs, key) => ({
    'watt': _.sumBy(objs, 'watt')
  }))
  .value()

console.log(getWatt)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

如您所见,我返回了一组 0 值。但是,我想取回377 的结果值。有什么建议我做错了吗?

感谢您的回复!

【问题讨论】:

    标签: javascript lodash


    【解决方案1】:

    使用纯js很容易

    const sum = allProducts.reduce((a, {watt}) => a + watt, 0);
    console.log(sum);
    <script>
    let allProducts = [{
        "unique_id": "102",
        "currency": "$",
        "price": "529.99",
        "watt": 150
      },
      {
        "unique_id": "11",
        "currency": "$",
        "price": "323",
        "watt": 150
      },
      {
        "unique_id": "13",
        "currency": "$",
        "price": "23",
        "watt": 77
      }
    ]
    
    
    </script>

    【讨论】:

      【解决方案2】:

      我已阅读答案,但我想为您的问题添加解释。你得到一个数组的原因是因为使用了.map。正如map 返回的数组不是单个元素。此外,您期望得到修改后的返回数组,而map 不会这样做。

      您想要实现的是可以使用.reduce 来完成。我的意思是这就是为什么有.reduce

      let allProducts = [{
          "unique_id": "102",
          "currency": "$",
          "price": "529.99",
          "watt": 150
        },
        {
          "unique_id": "11",
          "currency": "$",
          "price": "323",
          "watt": 150
        },
        {
          "unique_id": "13",
          "currency": "$",
          "price": "23",
          "watt": 77
        }
      ];
      var getWatt = allProducts.reduce((acc,curr)=> acc + curr.watt,0);
      console.log(getWatt);
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"&gt;&lt;/script&gt;

      【讨论】:

        【解决方案3】:

        尝试以下方法:

        let allProducts = [{
            "unique_id": "102",
            "currency": "$",
            "price": "529.99",
            "watt": 150
          },
          {
            "unique_id": "11",
            "currency": "$",
            "price": "323",
            "watt": 150
          },
          {
            "unique_id": "13",
            "currency": "$",
            "price": "23",
            "watt": 77
          }
        ];
        
        var sum = allProducts.reduce((sum,a)=>{
          return sum + a.watt;
        },0);
        console.log(sum);

        【讨论】:

          【解决方案4】:

          只需将单个 _.sumBy 与对象数组和所需的键 watt 相加即可。

          您的尝试使用对象,而不是 _.sumBy 的数组。对象不是数组,返回值为零。

          var allProducts = [{ unique_id: "102", currency: "$", price: "529.99", watt: 150 }, { unique_id: "11", currency: "$", price: "323", watt: 150 }, { unique_id: "13", currency: "$", price: "23", watt: 77 }],
              getWatt = _.sumBy(allProducts, 'watt');
          
          console.log(getWatt)
          &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"&gt;&lt;/script&gt;

          【讨论】:

            猜你喜欢
            • 2015-11-28
            • 2022-11-19
            • 1970-01-01
            • 2022-12-04
            • 1970-01-01
            • 2016-09-05
            • 1970-01-01
            • 1970-01-01
            • 2021-08-09
            相关资源
            最近更新 更多