【问题标题】:Add the missing Object's by comparing Two array of object通过比较两个对象数组添加丢失的对象
【发布时间】:2023-01-05 20:08:05
【问题描述】:

我正在尝试绘制一个图表来显示品牌的年度销售额比较,下面是两个年度销售额数组。

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"
  }
];

由于一些公司名称在各自的数组中丢失,我无法按预期绘制图表。我需要帮助将缺少的公司名称添加到相应的数组中,包括年份、名称和总数。 类似于这张图https://apexcharts.com/react-chart-demos/line-charts/data-labels/

期待 -

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

【问题讨论】:

    标签: javascript arrays charts apexcharts


    【解决方案1】:

    这是一个可以帮助你的例子

    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 = {};
    const missingData = {};
    
    for (const item of current_year) {
      currentYearData[item.comapanyName] = item;
    }
    
    for (const item of last_year) {
      if (currentYearData.hasOwnProperty(item.comapanyName)) {
        // Company exists in both current_year and last_year
        currentYearData[item.comapanyName] = item;
      } else {
        // Company exists in last_year but not in current_year
        missingData[item.comapanyName] = item;
      }
    }
    
    // Add the missing company names to the respective arrays
    for (const key in missingData) {
      if (!currentYearData.hasOwnProperty(key)) {
        current_year.push({ total: 0, comapnyName: key, year: 2021 });
      }
    }
    
    for (const key in currentYearData) {
      if (!missingData.hasOwnProperty(key)) {
        last_year.push({ total: 0, comapnyName: key, year: 2020 });
      }
    }
    
    console.log(current_year);
    console.log(last_year);
    

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 1970-01-01
      • 2020-08-28
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多