簡體   English   中英

通過比較 object 的兩個數組添加丟失的對象

[英]Add the missing Object's by comparing Two array of object

我正在嘗試 plot 一張圖表來顯示品牌的年度銷售額比較,下面是兩個 arrays 的年度銷售額。

var current_year = [
  {
    total: 12941.17,
    comapanyName: "Samsung",
    year: "2021"
  },
  {
    total: 17946.87,
    comapanyName: "Haier",
    year: "2021"
  },
  {
    total: 3832.36,
    comapanyName: "Beetel",
    year: "2021"
  },
  {
    total: 12528,
    comapanyName: "Celkon",
    year: "2021"
  }
];
var last_year = [
  {
    total: 427805.51,
    comapanyName: "Samsung",
    year: "2020"
  },
  {
    total: 77576.33,
    comapanyName: "Godrej",
    year: "2020"
  },
  {
    total: 53389.02,
    comapanyName: "Beetel",
    year: "2020"
  },
  {
    total: 100748.49,
    comapanyName: "Celkon",
    year: "2020"
  },
  {
    total: 4534.19,
    comapanyName: "FORD",
    year: "2020"
  },
  {
    total: 5.05,
    comapanyName: "Voltas",
    year: "2020"
  }
];

由於 arrays 中缺少一些公司名稱,因此我無法按預期對圖表進行 plot。 我需要幫助將缺少的公司名稱添加到相應的數組中,包括年份、名稱和總數。 類似於這張圖表https://apexcharts.com/react-chart-demos/line-charts/data-labels/

期待 -

  1. 公司“FORD”在 last_year 中存在,但在 current_year 數組中缺失,在 current_year 數組中添加“FORD”Object 示例 = [{total:0, comapnyName:'FORD', year:2021}]
  2. 公司“Haier”在 current_year 中存在,但在 last_year 數組中缺失,在 last_year 數組中添加“Haier”示例 = [{total:0, comapnyName:"Haier", year:2020}]

這是一個可以幫助你的例子

var current_year = [
  {
    total: 12941.17,
    comapanyName: "Samsung",
    year: "2021"
  },
  {
    total: 17946.87,
    comapanyName: "Haier",
    year: "2021"
  },
  {
    total: 3832.36,
    comapanyName: "Beetel",
    year: "2021"
  },
  {
    total: 12528,
    comapanyName: "Celkon",
    year: "2021"
  }
];
var last_year = [
  {
    total: 427805.51,
    comapanyName: "Samsung",
    year: "2020"
  },
  {
    total: 77576.33,
    comapanyName: "Godrej",
    year: "2020"
  },
  {
    total: 53389.02,
    comapanyName: "Beetel",
    year: "2020"
  },
  {
    total: 100748.49,
    comapanyName: "Celkon",
    year: "2020"
  },
  {
    total: 4534.19,
    comapanyName: "FORD",
    year: "2020"
  },
  {
    total: 5.05,
    comapanyName: "Voltas",
    year: "2020"
  }
];
        const currentYearData = {};

        for (const item of current_year) {
          currentYearData[item.comapanyName] = true;
        }

        for (const item of last_year) {
          if (!currentYearData.hasOwnProperty(item.comapanyName) && item.comapanyName) {
            // Company exists in last_year but not in current_year
            current_year.push({ total: 0, companyName: item.comapanyName, year: "2021" });
          }
        }

        for (const item of current_year) {
          if (!last_year.find(c => c.comapanyName === item.comapanyName) && item.comapanyName) {
            // Company exists in current_year but not in last_year
            last_year.push({ total: 0, companyName: item.comapanyName, year: "2020" });
          }
        }

        console.log(current_year);
        console.log(last_year);

暫無
暫無

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

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