0👍
✅
Assuming you get the data as an object from the data base, you could iterate the objects and take the properties for new object.
For assigning a value, you could take SEASON
for addressing the related ingredients.
var data = [
{ ROOM_ID: 1234567, SEASON: 1, ING_S1: 35, ING_S2: null, ING_S3: null },
{ ROOM_ID: 1234567, SEASON: 2, ING_S1: null, ING_S2: 24, ING_S3: null },
{ ROOM_ID: 1234567, SEASON: 3, ING_S1: null, ING_S2: null, ING_S3: 15 },
{ ROOM_ID: 2233445, SEASON: 2, ING_S1: null, ING_S2: 60, ING_S3: null },
{ ROOM_ID: 2233445, SEASON: 3, ING_S1: null, ING_S2: null, ING_S3: 41 }
],
result = {};
data.forEach(({ ROOM_ID, SEASON, ING_S1, ING_S2, ING_S3 }) => {
result[ROOM_ID] = result[ROOM_ID] || { roomName: 'blabla', ingredientsS1: '', ingredientsS2: '', ingredientsS3: '' };
result[ROOM_ID]['ingredientsS' + SEASON] = [ING_S1, ING_S2, ING_S3][SEASON - 1];
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Source:stackexchange.com