簡體   English   中英

如何在 lodash 中對數據進行分組並使用可選屬性?

[英]how to group data and using optional attribute in lodash?

我正在使用 lodash 進行數據操作,但是當我需要使用可選屬性對數據進行分組和聚合數據時,我被卡住了。

我有以下數據,我希望通過這些數據低於 output:

var data = [{product: 'Matcha Latte', year: '2015', score:'43.3'},{product: 'Milk Tea', year: '2015', score:'22'},
    {product: 'Milk Tea', year: '2015', score:'33.1'},{product: 'Milk Tea', year: '2015', score:'28'},
    {product: 'Cheese Cocoa', year: '2015', score:'86.4'},{product: 'Walnut Brownie', year: '2015', score:'72.4'},
    {product: 'Matcha Latte', year: '2016', score:'40'},{product: 'Matcha Latte', year: '2016', score:'45.8'},
    {product: 'Matcha Latte', year: '2017', score:'33.3'},{product: 'Matcha Latte', year: '2017', score:'43.3'},
    {product: 'Matcha Latte', year: '2017', score:'16.73'},{product: 'Milk Tea', year: '2016', score:'73.4'},
    {product: 'Milk Tea', year: '2017', score:'50'},{product: 'Milk Tea', year: '2017', score:'.1'},
    {product: 'Walnut Brownie', year: '2016', score:'50.4'},{product: 'Walnut Brownie', year: '2016', score:'3.6'},
    {product: 'Cheese Cocoa', year: '2016', score:'65.2'},{product: 'Cheese Cocoa', year: '2017', score:'82.5'},
    {product: 'Walnut Brownie', year: '2017', score:'39.1'}];

和預期的 output:

[{product: 'Matcha Latte', '2015': 43.3, '2016': 85.8, '2017': 93.7},
        {product: 'Milk Tea', '2015': 83.1, '2016': 73.4, '2017': 55.1},
        {product: 'Cheese Cocoa', '2015': 86.4, '2016': 65.2, '2017': 82.5},
        {product: 'Walnut Brownie', '2015': 72.4, '2016': 53.9, '2017': 39.1}]

我嘗試了以下但如何進一步進行:

 var ans = _(data)
.groupBy('product')
.value();

使用純 javascript 您可以減少它:

 var data = [{product: 'Matcha Latte', year: '2015', score:'43.3'},{product: 'Milk Tea', year: '2015', score:'22'}, {product: 'Milk Tea', year: '2015', score:'33.1', mat:50},{product: 'Milk Tea', year: '2015', score:'28', mat:50}, {product: 'Cheese Cocoa', year: '2015', score:'86.4'},{product: 'Walnut Brownie', year: '2015', score:'72.4'}, {product: 'Matcha Latte', year: '2016', score:'40'},{product: 'Matcha Latte', year: '2016', score:'45.8'}, {product: 'Matcha Latte', year: '2017', score:'33.3'},{product: 'Matcha Latte', year: '2017', score:'43.3'}, {product: 'Matcha Latte', year: '2017', score:'16.73'},{product: 'Milk Tea', year: '2016', score:'73.4'}, {product: 'Milk Tea', year: '2017', score:'50'},{product: 'Milk Tea', year: '2017', score:'.1'}, {product: 'Walnut Brownie', year: '2016', score:'50.4'},{product: 'Walnut Brownie', year: '2016', score:'3.6'}, {product: 'Cheese Cocoa', year: '2016', score:'65.2'},{product: 'Cheese Cocoa', year: '2017', score:'82.5'}, {product: 'Walnut Brownie', year: '2017', score:'39.1'}]; var result = Object.values(data.reduce((acc, {product, year, score, mat})=>{ acc[product] = acc[product] || {product}; acc[product][year] = (acc[product][year] || 0) + Number(score); if(mat) acc[product][`${year}_mat`] = (acc[product][`${year}_mat`] || 0) + mat; return acc; },{})); console.log(result);

將數據格式化為具有年份鍵和值的對象。 product分組,然后 map 並合並每個組。 如果第一個值 ( a ) 是數字,則在合並時,將其與第二個值 ( b ) 合並。 如果不返回undefined ,則將使用_.mergeWith()使用的標准合並算法。

 const data = [{"product":"Matcha Latte","year":"2015","score":"43.3"},{"product":"Milk Tea","year":"2015","score":"22"},{"product":"Milk Tea","year":"2015","score":"33.1"},{"product":"Milk Tea","year":"2015","score":"28"},{"product":"Cheese Cocoa","year":"2015","score":"86.4"},{"product":"Walnut Brownie","year":"2015","score":"72.4"},{"product":"Matcha Latte","year":"2016","score":"40"},{"product":"Matcha Latte","year":"2016","score":"45.8"},{"product":"Matcha Latte","year":"2017","score":"33.3"},{"product":"Matcha Latte","year":"2017","score":"43.3"},{"product":"Matcha Latte","year":"2017","score":"16.73"},{"product":"Milk Tea","year":"2016","score":"73.4"},{"product":"Milk Tea","year":"2017","score":"50"},{"product":"Milk Tea","year":"2017","score":".1"},{"product":"Walnut Brownie","year":"2016","score":"50.4"},{"product":"Walnut Brownie","year":"2016","score":"3.6"},{"product":"Cheese Cocoa","year":"2016","score":"65.2"},{"product":"Cheese Cocoa","year":"2017","score":"82.5"},{"product":"Walnut Brownie","year":"2017","score":"39.1"}]; const result = _(data).map(({ year, score, ...rest }) => ({...rest, [year]: +score })) // create objects with the year as the key, and score as the value.groupBy('product').map(g => _.mergeWith({}, ...g, (a, b) => _.isNumber(a)? a + b: undefined)) // merge and sum up the score of each year, if it appears more than once.value(); console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM