简体   繁体   English

如何使用javascript填充数组中缺少的月份

[英]How to fill in missing months in an array using javascript

Is there a way how can i complete missing month and sale in an incomplete array. 有没有办法如何在一个不完整的数组中完成丢失月份和销售。

sometimes i get a query like this: 有时我得到这样的查询:

var sales = [

    {'month': '04', 'sale': 126},
    {'month': '06', 'sale': 165},
    {'month': '07', 'sale': 10},
    {'month': '08', 'sale': 20},
    {'month': '09', 'sale': 211},
    {'month': '10', 'sale': 27},
    {'month': '11', 'sale': 112},
];

and i need to add the missing months with sale: 0. 我需要在销售中添加缺少的月份:0。

I thought i can make a second array with all months and then compare this two arrays and pick the duplicates to the array with all months: 我想我可以制作一个包含所有月份的第二个数组,然后将这两个数组进行比较并选择重复数据到所有月份的数组:

var compareArray = [

    {'month': '01', 'sale': 0},
    {'month': '02', 'sale': 0},
    {'month': '03', 'sale': 0},
    {'month': '04', 'sale': 0},
    {'month': '05', 'sale': 0},
    {'month': '06', 'sale': 0},
    {'month': '07', 'sale': 0},
    {'month': '08', 'sale': 0},
    {'month': '09', 'sale': 0},
    {'month': '10', 'sale': 0},
    {'month': '11', 'sale': 0},
    {'month': '12', 'sale': 0},
];

Instead of pre-defining the 12-entries array, you could use Array(12).keys() and use Array.from to map that to the desired output: 您可以使用Array(12).keys()并使用Array.from将其映射到所需的输出,而不是预先定义12个条目数组:

 var sales = [{'month': '04', 'sale': 126}, {'month': '06', 'sale': 165}, {'month': '07', 'sale': 10}, {'month': '08', 'sale': 20}, {'month': '09', 'sale': 211}, {'month': '10', 'sale': 27}, {'month': '11', 'sale': 112}]; sales = Array.from(Array(12).keys(), month => sales.find(sale => +sale.month === month+1) || { month: ("0"+(month+1)).substr(-2), sale: 0 } ); console.log(sales); 

An approach based on .map and .find array methods 一种基于.map和.find数组方法的方法

const data = [
    {'month': '04', 'sale': 126},
    {'month': '06', 'sale': 165},
    {'month': '07', 'sale': 10},
    {'month': '08', 'sale': 20},
    {'month': '09', 'sale': 211},
    {'month': '10', 'sale': 27},
    {'month': '11', 'sale': 112},
];

const result = [...Array(12)].map((m, i) => {
  const month = i < 9 ? '0' + (i + 1) : String(i + 1);
  return data.find(d => d.month === month) || { month, sale: 0 };
});

You can use a simple for loop then Array.sort() to get the desired output: 你可以使用一个简单的for循环然后使用Array.sort()来获得所需的输出:

 var sales = [{ 'month': '04', 'sale': 126 }, { 'month': '06', 'sale': 165 }, { 'month': '07', 'sale': 10 }, { 'month': '08', 'sale': 20 }, { 'month': '09', 'sale': 211 }, { 'month': '10', 'sale': 27 }, { 'month': '11', 'sale': 112 }, ]; for (var i = 1; i <= 12; i++) { var existObj = sales.find(item => +item.month === i); if (!existObj) { sales.push({ 'month': i > 9 ? i : '0' + i, 'sale': 0 }); } } sales.sort(function(a, b) { return +a.month - +b.month; }); console.log(sales); 

Because your month numbers are numbers, and span a convenient range, you can use them as array indexes: 因为您的月份数字数字,并且跨越方便的范围,您可以将它们用作数组索引:

var yearSales = sales.reduce(function(rv, month) {
  a[parseInt(month.month, 10)] = month;
  return a;
}, []);

Then you can find the empty slots: 然后你可以找到空槽:

for (let i = 0; i < 12; i++)
  if (!yearSales[i])
    yearSales[i] = { month: i, sales: 0 };

(I didn't use .forEach() because it skips empty slots, and those are the ones I want to target.) (我没有使用.forEach()因为它会跳过空插槽,而那些是我想要定位的插槽。)

You can use the same approach I used below. 您可以使用我在下面使用的相同方法。 Bascially just loop 1 - 12 and if you find missing months, add them to the array. 基本上只是循环1 - 12,如果您发现缺少月份,请将它们添加到阵列中。 Once you have all your months in the array, simply sort in ascending order based on month number. 一旦您在阵列中拥有所有月份,只需根据月份数按升序排序。

 const sales = [{ 'month': '04', 'sale': 126 }, { 'month': '06', 'sale': 165 }, { 'month': '07', 'sale': 10 }, { 'month': '08', 'sale': 20 }, { 'month': '09', 'sale': 211 }, { 'month': '10', 'sale': 27 }, { 'month': '11', 'sale': 112 } ]; function generateSalesReport(salesData) { const monthsWithSales = []; const salesReport = []; salesData.forEach(el => { monthsWithSales.push(Number.parseInt(el.month)); salesReport.push(el); }); //Fill in the missing months for (let i = 1; i <= 12; i++) { if (monthsWithSales.indexOf(i) === -1) { let monthStr = i.toString(); salesReport.push({ 'month': monthStr.length < 2 ? '0' + monthStr : monthStr, 'sale': 0 }); } } //Sort the sales report array return salesReport.sort((a, b) => parseInt(a.month) - parseInt(b.month)); } console.log(generateSalesReport(sales)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM