Stock Calculation to be start from given date
My Sql query is sum stock values for date wise and I stored in value variable.
let value = [
{ place: 'Bangalore', production:16, stock: -3, date: '2021-06-26' },
{ place: 'Bangalore', production:2.5, stock: 13, date: '2021-06-27' },
{ place: 'Bangalore', production:3, stock: 15.5, date: '2021-06-28' },
{ place: 'Bangalore', production:5.5, stock: 18.5, date: '2021-06-29' },
{ place: 'Bangalore', production:4, stock: 24, date: '2021-06-30' },
{ place: 'Bangalore', production:7, stock: 28, date: '2021-07-09' },
{ place: 'Bangalore', production:9, stock: 35, date: '2021-07-10' }
]
I want accumulation of previous day stock+production=today stock. Its working as expected below script. But I have to calculate if middle of date production value change.
value.reduce((acc, cur) => {
const newArr = acc + cur.stock;
cur.stock = acc;
return newArr;
}, 0);
console.log("value::::",value)
If I update any of the production value in middle of date it has to calculate stock from that date only.
eg expected results: Suppose If edit 2021-06-29 production:5
outcome:
0: {place: "Bangalore", production: 16, stock: -3, date: "2021-06-26"}
1: {place: "Bangalore", production: 2.5, stock: 13, date: "2021-06-27"}
2: {place: "Bangalore", production: 3, stock: 15.5, date: "2021-06-28"}
3: {place: "Bangalore", production: 6.5, stock: 18.5, date: "2021-06-29"}
4: {place: "Bangalore", production: 4, stock: 25, date: "2021-06-30"}
5: {place: "Bangalore", production: 7, stock: 29, date: "2021-07-09"}
6: {place: "Bangalore", production: 9, stock: 36, date: "2021-07-10"}
Kindly suggest your ideas. Its greatly appreciated
let value = [
{ place: 'Bangalore', production:16, stock: -3, date: '2021-06-26' },
{ place: 'Bangalore', production:2.5, stock: 13, date: '2021-06-27' },
{ place: 'Bangalore', production:3, stock: 15.5, date: '2021-06-28' },
{ place: 'Bangalore', production:5.5, stock: 18.5, date: '2021-06-29' },
{ place: 'Bangalore', production:4, stock: 24, date: '2021-06-30' },
{ place: 'Bangalore', production:7, stock: 28, date: '2021-07-09' },
{ place: 'Bangalore', production:9, stock: 35, date: '2021-07-10' }
]
value.reduce((acc, cur) => {
const newArr = acc + cur.stock;
cur.stock = acc;
return newArr;
}, 0);
console.log("value::::",value)
Comments
Post a Comment