I have an array of objects:
const res = [
{
KIND_ID: 2229,
NAME: "Group",
SKU_ID: 311,
ARTICLE_NAME: "Product one",
WEEK: 202027,
FORECAST: 81928.37,
PROMO: 0,
SALES: 92848,
SAFETY_STOCK: 57704.79,
STORES_COUNT: 132,
ADD_FCST: null
},
{
KIND_ID: 2229,
NAME: "Group",
SKU_ID: 311,
ARTICLE_NAME: "Product one",
WEEK: 202028,
FORECAST: 84278.85,
PROMO: 0,
SALES: null,
SAFETY_STOCK: 64000.39,
STORES_COUNT: 144,
ADD_FCST: null
},
{
KIND_ID: 2229,
NAME: "Group",
SKU_ID: 316,
ARTICLE_NAME: "Product two",
WEEK: 202027,
FORECAST: 89112.97,
PROMO: 0,
SALES: 98007,
SAFETY_STOCK: 59509.31,
STORES_COUNT: 142,
ADD_FCST: null
},
{
KIND_ID: 2229,
NAME: "Group",
SKU_ID: 316,
ARTICLE_NAME: "Product two",
WEEK: 202028,
FORECAST: 85129.4,
PROMO: 0,
SALES: null,
SAFETY_STOCK: 63409.61,
STORES_COUNT: 144,
ADD_FCST: null
}
];
I want to group some fields in WEEKS, e.g:
const expected = [
{
KIND_ID: 2229,
NAME: "Group",
SKU_ID: 311,
ARTICLE_NAME: "Product one",
FORECAST: [{ 202026: 97555.64 }, { 202027: 98336.45 }],
PROMO: [{ 202026: 0 }, { 202027: 0 }],
SALES: [{ 202026: 95911.77 }, { 202027: null }],
SAFETY_STOCK: [{ 202026: 63622.28 }, { 202027: 72852.62 }],
STORES_COUNT: [{ 202026: 135 }, { 202027: 143 }],
ADD_FCST: [{ 202026: null }, { 202027: null }]
},
{
KIND_ID: 2229,
NAME: "Group",
SKU_ID: 316,
ARTICLE_NAME: "Product two",
FORECAST: [{ 202026: 104125.69 }, { 202027: 101147.07 }],
PROMO: [{ 202026: 0 }, { 202027: 0 }],
SALES: [{ 202026: 102281 }, { 202027: null }],
SAFETY_STOCK: [{ 202026: 53709.55 }, { 202027: 60675.26 }],
STORES_COUNT: [{ 202026: 143 }, { 202027: 144 }],
ADD_FCST: [{ 202026: null }, { 202027: null }]
}
];
If it helps i have array of weeks and values need to be grouped:
const weeks = [202027, 202028];
const keys = ['FORECAST','PROMO','SALES','SAFETY_STOCK','STORES_COUNT','ADD_FCST'];
I tried to solve with reduce and underscore method groupBy but this not helped. How to group this res array to expected?
Dummy text, because: It looks like your post is mostly code; please add some more details. But that's all what i want to tell