簡體   English   中英

使用.reduce函數進行某些計算並返回帶有數組結果的對象

[英]Using the .reduce function to made certain calculations and return an object with array results

因此,我成功地獲得了所追求的結果:

 const days = [ { date: '2016-12-13T00:00:00.000Z', stats: [ { name: 'Soft Drinks', sold: 34, }, { name: 'Snacks', sold: 3, }, { name: 'Coffee and warm drinks', sold: 26, }, ], }, { date: '2016-12-14T00:00:00.000Z', stats: [ { name: 'Soft Drinks', sold: 34, }, { name: 'Snacks', sold: 3, }, { name: 'Coffee and warm drinks', sold: 26, }, ], }, ]; const newStats = days.reduce(function (pastDay, currentDay) { const nextStats = currentDay.stats.map(function(stat) { const oldSold = pastDay.stats.find(function (old) { return old.name === stat.name; }); const newSold = stat.sold + oldSold.sold; stat.sold = newSold; return stat; }); return { stats: nextStats, }; }); console.log(newStats); 

輸出:

{
  "stats": [
    {
      "name": "Soft Drinks",
      "sold": 68
    },
    {
      "name": "Snacks",
      "sold": 6
    },
    {
      "name": "Coffee and warm drinks",
      "sold": 52
    }
  ]
}

這正是我所追求的,但是當處理不同的days數組時,它遵循對象數組的相同結構。 我在pastDay上收到未定義的錯誤,有人可以幫助我發現問題嗎? 或幫助我找到.reduce替代方法

我遇到麻煩的數組:

 const days = [ { "_id":{ "_str":"f23f02994ab992437e423e24" }, "date":"2016-12-13T00:00:00.000Z", "statistics":{ "breakdown":{ "byTurnover":[ { "name":"Soft Drinks", "sold":34, "percentage":31.14 }, { "name":"Snacks", "sold":3, "percentage":2.65 }, { "name":"Coffee and warm drinks", "sold":26, "percentage":21.54 }, { "name":"Brandy", "sold":2, "percentage":2.75 }, { "name":"Beer", "sold":20, "percentage":20.15 }, { "name":"Mixed drinks Other", "sold":21, "percentage":21.77 } ], } }, "id":{ "_str":"f23f02994ab992437e423e24" } }, { "_id":{ "_str":"b3d0ad7f314e33021739f70c" }, "date":"2016-12-14T00:00:00.000Z", "statistics":{ "breakdown":{ "byTurnover":[ { "name":"Soft Drinks", "sold":34, "percentage":31.14 }, { "name":"Snacks", "sold":3, "percentage":2.65 }, { "name":"Coffee and warm drinks", "sold":26, "percentage":21.54 }, { "name":"Brandy", "sold":2, "percentage":2.75 }, { "name":"Beer", "sold":20, "percentage":20.15 }, { "name":"Mixed drinks Other", "sold":21, "percentage":21.77 } ], } }, "id":{ "_str":"b3d0ad7f314e33021739f70c" } }, { "_id":{ "_str":"e1906ce07ab811c74528e3cc" }, "date":"2016-12-15T00:00:00.000Z", "statistics":{ "breakdown":{ "byTurnover":[ { "name":"Soft Drinks", "sold":34, "percentage":31.14 }, { "name":"Snacks", "sold":3, "percentage":2.65 }, { "name":"Coffee and warm drinks", "sold":26, "percentage":21.54 }, { "name":"Brandy", "sold":2, "percentage":2.75 }, { "name":"Beer", "sold":20, "percentage":20.15 }, { "name":"Mixed drinks Other", "sold":21, "percentage":21.77 } ], } }, "id":{ "_str":"e1906ce07ab811c74528e3cc" } }, ]; const newStats = days.reduce(function (pastDay, currentDay) { const nextStats = currentDay.statistics.breakdown.byTurnover.map(function(stat) { const oldSold = pastDay.statistics.breakdown.byTurnover.find(function (old) { return old.name === stat.name; }); const newSold = stat.sold + oldSold.sold; stat.sold = newSold; return stat; }); return { stats: nextStats, }; }); console.log(newStats); 

輸出: Uncaught TypeError: Cannot read property 'breakdown' of undefined

第二個數組的.reduce代碼:

const newStats = days.reduce(function (pastDay, currentDay) {
  const nextStats = currentDay.statistics.breakdown.byTurnover.map(function(stat) {
    const oldSold = pastDay.statistics.breakdown.byTurnover.find(function (old) {
        return old.name === stat.name;
    });

    const newSold = stat.sold + oldSold.sold;
    stat.sold = newSold;
    return stat;
  });

  return {
    stats: nextStats,
  };
});

console.log(newStats);

您的第一個 reducer返回的對象格式與輸入數組匹配,如

return {
    stats: nextStats,
};

您的數組如下所示:

const days = [{ stats: [...] }]

因此,當您的內部循環將.stats作為數組進行迭代時,它將正確運行。

您的第二個 reducer迭代具有以下結構的對象:

const days = [{ statistics: { breakdown: { byTurnover: [...] } }]

但是然后返回與該結構不匹配的對象:

return {
    stats: nextStats,
};

因此,reducer的第一個迭代將起作用,然后將運行第二個迭代,並且第一個參數pastDay將是上一次運行的返回值,它將沒有您要查找的任何鍵。

一個快速而骯臟的解決方案就是在返回時匹配對象鍵的深度:

const newStats = days.reduce(function (pastDay, currentDay) {
    const nextStats = currentDay.statistics.breakdown.byTurnover.map(function(stat) {
        const oldSold = pastDay.statistics.breakdown.byTurnover.find(function (old) {
            return old.name === stat.name;
        });

        const newSold = stat.sold + oldSold.sold;
        stat.sold = newSold;
        return stat;
    });

    return {
        statistics: { breakdown: { byTurnover: nextStats } },
    };
});

雖然這回答了問題,但您難以遵循所使用的邏輯。 根據您要完成的工作(代碼尚不清楚),這可能不是理想的方法。

暫無
暫無

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

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