[{
month: 2,
sum: 10
}, {
month: 3,
sum: 20
},{
month: 4,
sum: 26
}, {
month: 5,
sum: 30
}, {
month: 7,
sum: 22
}, {
month: 8,
sum: 25
},]
['', '10 ', '20', '26', '30', '', '22', '25', '', '', '', '']
const newArr = arr.reduce((acc, n) => (
acc[n.month - 1] = n.sum,
acc
), Array(12).fill(''));
const newArr = Array.from({ length: 12 }, function(_, i) {
return this[-~i] || '';
}, Object.fromEntries(arr.map(n => [ n.month, n.sum ])));
const newArr = [];
for (let i = 0, j = 0; i < 12; i++) {
newArr.push((arr[j] || {}).month === i + 1 ? arr[j++].sum : '');
}
// data = массив такого вида
Array(12).fill().map((item, index) => {
const month = data.find(m => m.month === index + 1)
return month ? month.sum : ''
})
// data = массив такого вида
result = Array(12).fill('')
for(let i = 0; i < data.length; i++) {
result[data[i].month - 1] = data[i].sum
}