代码之家  ›  专栏  ›  技术社区  ›  Carol.Kar

带对象的汇总数组

  •  1
  • Carol.Kar  · 技术社区  · 7 年前

    我有一个包含对象的数组,我想总结一下 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 . 有什么建议我做错了什么?

    谢谢你的回复!

    4 回复  |  直到 7 年前
        1
  •  4
  •   baao    7 年前

    使用简单的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
  •  2
  •   Muhammad Usman    7 年前

    我已经看过答案了,但我想给你的问题加一个解释。获取数组的原因是 .map . AS map 返回数组而不是单个元素。此外,您希望将返回的数组修改为 地图 不会那么做的。

    你要达到的目标是 .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);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
        3
  •  1
  •   amrender singh    7 年前

    请尝试以下操作:

    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
  •  1
  •   Nina Scholz    7 年前

    就拿一张 _.sumBy 使用对象数组和所需的密钥 watt 为了总结。

    尝试使用对象,而不是数组 _苏姆比 . 对象不是数组,返回值为零。

    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)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>