简体   繁体   English

通过比较 object 的两个数组添加丢失的对象

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

I am trying to plot a chart to display sales comparison by year for the brands, below are the two arrays of sales by year.我正在尝试 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"
  }
];

Since some company names are missing in respective arrays I'm not able to plot the chart as expected.由于 arrays 中缺少一些公司名称,因此我无法按预期对图表进行 plot。 I need help in adding the missing company name in respective array with year, name and total.我需要帮助将缺少的公司名称添加到相应的数组中,包括年份、名称和总数。 Similar to this chart https://apexcharts.com/react-chart-demos/line-charts/data-labels/类似于这张图表https://apexcharts.com/react-chart-demos/line-charts/data-labels/

Expectation -期待 -

  1. company "FORD" is present in last_year but missing in current_year array, Add the "FORD" Object in current_year array Example = [{total:0, comapnyName:'FORD', year:2021}]公司“FORD”在 last_year 中存在,但在 current_year 数组中缺失,在 current_year 数组中添加“FORD”Object 示例 = [{total:0, comapnyName:'FORD', year:2021}]
  2. company "Haier" is present in current_year but missing in last_year array, Add the "Haier" in last_year array Example = [{total:0, comapnyName:"Haier", year:2020}]公司“Haier”在 current_year 中存在,但在 last_year 数组中缺失,在 last_year 数组中添加“Haier”示例 = [{total:0, comapnyName:"Haier", year:2020}]

here is an example that can help you这是一个可以帮助你的例子

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.

相关问题 与另一个数组比较后如何添加缺失对象的值? - How to add value of missing object after comparing to another array? 比较 object 的两个数组以返回未找到的值 - Comparing two array of object to return values that are not found 比较两个对象数组并用另一个替换 object - Comparing two array of objects and replacing an object with another 比较JavaScript中对象数组的两个参数 - Comparing two parameters of an object array in JavaScript 比较两个数组并匹配第二个数组对象中第一个数组对象的一个键,如果找到则插入一个键和值 - comparing two arrays and match one key from first array's object in the second array's object if found then insert a key and value 如何通过与模型比较将特定缺少的字段添加到json对象 - How to add specific missing fields to a json object by comparing with a model 如何在比较数组和 object 时检查缺失的字段? - How do I check the missing fields while comparing an array and an object? 比较两个对象的 arrays,如果缺少某个键的值,则将具有该值的 object 添加到数组中 - 两种方式 - Compare two arrays of objects and if value for a certain key is missing, add an object with that value to the array - two ways 比较 object 键/值对中没有标准键的两个 Arrays,将缺失值推送到 javascript 中的第一个数组 - Comparing Two Arrays that don't have standard keys in the object key/value pairs, pushing missing values to first array in javascript 将“this”对象与DOM Array中的Object进行比较 - Comparing “this” object with Object in DOM Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM