React Data Convert to Tree Data - javascript

Here is my goal :
1.check if the object has children(it might not has children). if has children property. then move the children object in object which is the same level originally.
ex:
const data = {
bonusCode: "ALICELTTEST001",
bonusName: "test",
numberOfBonusUser: 2,
bonusAmountClaim: 100100.0,
baseBonusAmountClaim: 348675821.64956486,
children: [
{
siteId: 1,
currencyId: 4,
currency: "THB",
bonusCode: "VipRebate",
bonusName: null,
numberOfBonusUser: 22,
bonusAmountClaim: 813.76,
baseBonusAmountClaim: 6402.5177026,
},
],
};
2.add a new property currency to parent. and it value is bonusName's value
ex:
const data = {
currency: "test",
bonusCode: "ALICELTTEST001",
bonusName: "test",
numberOfBonusUser: 2,
bonusAmountClaim: 100100.0,
baseBonusAmountClaim: 348675821.64956486,
children: [
{
siteId: 1,
currencyId: 4,
currency: "THB",
bonusCode: "VipRebate",
bonusName: null,
numberOfBonusUser: 22,
bonusAmountClaim: 813.76,
baseBonusAmountClaim: 6402.5177026,
},
],
};
Here is my original Data:
const data = {
groupByBonusCode: [
{
groupInfo: {
bonusCode: "ALICELTTEST001",
bonusName: "alice lottery test",
numberOfBonusUser: 2,
bonusAmountClaim: 100100.0,
baseBonusAmountClaim: 348675821.64956486,
},
children: [
{
siteId: 1,
currencyId: 15,
currency: "IDR",
bonusCode: "ALICELTTEST001",
bonusName: "alice lottery test",
numberOfBonusUser: 1,
bonusAmountClaim: 100000.0,
baseBonusAmountClaim: 348675034.86750349,
},
{
siteId: 1,
currencyId: 4,
currency: "THB",
bonusCode: "ALICELTTEST001",
bonusName: "alice lottery test",
numberOfBonusUser: 1,
bonusAmountClaim: 100.0,
baseBonusAmountClaim: 786.78206137,
},
],
},
{
groupInfo: {
bonusCode: "VipRebate",
bonusName: null,
numberOfBonusUser: 80,
bonusAmountClaim: 6613.89,
baseBonusAmountClaim: 19024613.52987081,
},
children: [
{
siteId: 1,
currencyId: 4,
currency: "THB",
bonusCode: "VipRebate",
bonusName: null,
numberOfBonusUser: 22,
bonusAmountClaim: 813.76,
baseBonusAmountClaim: 6402.5177026,
},
],
},
],
};
I've been thinking a while. Thanks a lot !!

Related

Remodel array of object using reduce

I want to change the structure of the current Array that I am consuming for a better use case, I want to group items into subarrays in this order. soccer > segment name > league > fixtures . I have been stuck on this for hours. I tried to use reduce but none of my attempts was close to success. Below is the sample data and the result I am trying to achieve.
const currentData = [
{
"fixtureName": "Fulham vs Chelsea",
"league": "Premier League",
"sport": "Soccer",
"segmentName": "England",
"markets": []
},
{
"fixtureName": "Arsenal vs liverpool",
"league": "Premier League",
"sport": "Soccer",
"segmentName": "England",
"markets": []
}, {
"fixtureName": "Middlesbrough FC vs. West Bromwich Albion",
"league": "Championship",
"sport": "Soccer",
"segmentName": "England",
"markets": []
}, {
"fixtureName": "Club El Porvenir vs. Berazategui",
"league": "Primera C",
"sport": "Soccer",
"segmentName": "Argentina",
"markets": []
}, {
"fixtureName": "Lille vs Angers ",
"league": "Ligue 1",
"sport": "Soccer",
"segmentName": "France",
"markets": []
}, {
"fixtureName": "PSG vs Nantes",
"league": "Ligue 1",
"sport": "Soccer",
"segmentName": "France",
"markets": []
}, {
"fixtureName": "Argentino de Quilmes vs. Comunicaciones",
"league": "Primera B",
"sport": "Soccer",
"segmentName": "Argentina",
"markets": []
}, {
"fixtureName": "Deportivo Merlo vs. Club Atletico Fenix",
"league": "Primera B",
"sport": "Soccer",
"segmentName": "Argentina",
"markets": []
},
]
This is the type of output and result I want
const data = [
{
sportID: 1,
sportName: 'Soccer',
categories: [
{
itemName: 'Argentina',
tournaments: [
{
itemID: 1,
itemName: 'ARG - Primera B',
events: [
{
id: 1,
itemName: 'Argentino de Quilmes vs. Comunicaciones',
markets: [],
},
{
id: 2,
itemName: 'Deportivo Merlo vs. Club Atletico Fenix',
markets: [],
},
],
},
{
itemID: 2,
itemName: 'ARG - Primera C',
events: [
{
id: 1,
itemName: 'Club El Porvenir vs. Berazategui',
markets: [],
},
],
},
],
},
{
itemName: 'England',
tournaments: [
{
itemID: 1,
itemName: 'ENG - Premier League',
events: [
{
id: 1,
itemName: 'Arsenal vs Liverpool',
markets: [],
},
{
id: 2,
itemName: 'Fulham vs Chelsea',
markets: [],
},
],
},
{
itemID: 2,
itemName: 'ENG - Championship',
events: [
{
id: 1,
itemName: 'Middlesbrough FC vs. West Bromwich Albion',
markets: [],
},
],
},
],
},
{
itemName: 'France',
tournaments: [
{
itemID: 1,
itemName: 'Ligue 1',
events: [
{
id: 1,
itemName: 'PSG vs Nantes',
markets: [],
},
{
id: 2,
itemName: 'Lille vs Angers',
markets: [],
},
],
},
],
},
],
},
];
The expected results don't quite match the input, but here's a way to reduce each item to its location in the tree, by first creating the tree's objects if not already created.
var currentData = [{"fixtureName":"Fulham vs Chelsea","league":"Premier League","sport":"Soccer","segmentName":"England","markets":[]},{"fixtureName":"Arsenal vs liverpool","league":"Premier League","sport":"Soccer","segmentName":"England","markets":[]},{"fixtureName":"Middlesbrough FC vs. West Bromwich Albion","league":"Championship","sport":"Soccer","segmentName":"England","markets":[]},{"fixtureName":"Club El Porvenir vs. Berazategui","league":"Primera C","sport":"Soccer","segmentName":"Argentina","markets":[]},{"fixtureName":"Lille vs Angers ","league":"Ligue 1","sport":"Soccer","segmentName":"France","markets":[]},{"fixtureName":"PSG vs Nantes","league":"Ligue 1","sport":"Soccer","segmentName":"France","markets":[]},{"fixtureName":"Argentino de Quilmes vs. Comunicaciones","league":"Primera B","sport":"Soccer","segmentName":"Argentina","markets":[]},{"fixtureName":"Deportivo Merlo vs. Club Atletico Fenix","league":"Primera B","sport":"Soccer","segmentName":"Argentina","markets":[]}]
var obj = currentData.reduce(function(agg, item) {
agg[item.sport] ??= {}
agg[item.sport][item.segmentName] ??= {}
agg[item.sport][item.segmentName][item.league] ??= {}
agg[item.sport][item.segmentName][item.league][item.fixtureName] ??= []
agg[item.sport][item.segmentName][item.league][item.fixtureName].push(item)
return agg;
}, {});
console.log(obj)
.as-console-wrapper {
max-height: 100% !important;
}

How to change the format of an array of objects based on ParentID

I am trying to change the format of an array of objects based on a field called ParentID.
Here is a sample of data:
var JsonVal = "data": {
"Factorid": 197325,
"orders": [
{
"pID": 794522,
"Count": 1,
"ParentID": 794551,
"Description": "",
"productid": "1428539",
"UnitPrice": "300000",
},
{
"pID": 794525,
"Count": 1,
"ParentID": 794551,
"Description": "",
"productid": "1428543",
"UnitPrice": "600000",
},
{
"pID": 794550,
"Count": 2,
"ParentID": 0,
"Description": "",
"productid": "1428648",
"UnitPrice": "60000",
},
{
"pID": 794551,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428647",
"UnitPrice": "250000",
} ,
{
"pID": 794526,
"Count": 3,
"ParentID": 794550,
"Description": "",
"productid": "1428548",
"UnitPrice": "600000",
},
{
"pID": 794527,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428542",
"UnitPrice": "400000",
},
]
}
As you can see every object has a ParentID. I want to say that if ParentID is 0, it will be known as a parent and add a field call children which its value is null. However if ParentID is not 0 , it will mean that this Object is a child of another Object that its pID is the same as ParentID child Object.The final JSON should be like this :
"data": {
"Factorid": 197325,
"orders": [
{
"pID": 794550,
"Count": 2,
"ParentID": 0,
"Description": "",
"productid": "1428648",
"UnitPrice": "60000",
"children": "[{
'pID': 794526,
'Count':3,
'ParentID': 794550,
'Description': '',
'productid': '1428548',
'UnitPrice': '600000',
}]",
},
{
"pID": 794551,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428647",
"UnitPrice": "250000",
"children":"[
{
'pID': 794522,
'Count': 1,
'ParentID': 794551,
'Description': '',
'productid': '1428539',
'UnitPrice': '300000',
},
{
'pID': 794525,
'Count': 1,
'ParentID': 794551,
'Description': '',
'productid': '1428543',
'UnitPrice': '600000',
},
]",
} ,
{
"pID": 794527,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428542",
"UnitPrice": "400000",
"children":"",
},
]
}
children should be added to any object which is parent and also children is always a string field.
I wrote a code but I do not know how to handle it?
JsonVal.orders.forEach(orders => {
if(orders.ParentID == 0){
orders.children="";
}else{
if(orders.ParentID == new_rows){
JsonVal.orders = JsonVal.orders.filter(item => item.ParentID == orders.ParentID);
}
}
});
You basically filter your list of orders if it is a "rootOrder" which means that it has one or multiple children.
Next, you filter your list again by matching the ParentId to the root.pID
// input
const JsonVal = {
data: {
Factorid: 197325,
orders: [
{
pID: 794522,
Count: 1,
ParentID: 794551,
Description: "",
productid: "1428539",
UnitPrice: "300000",
},
{
pID: 794525,
Count: 1,
ParentID: 794551,
Description: "",
productid: "1428543",
UnitPrice: "600000",
},
{
pID: 794550,
Count: 2,
ParentID: 0,
Description: "",
productid: "1428648",
UnitPrice: "60000",
},
{
pID: 794551,
Count: 1,
ParentID: 0,
Description: "",
productid: "1428647",
UnitPrice: "250000",
},
{
pID: 794526,
Count: 3,
ParentID: 794550,
Description: "",
productid: "1428548",
UnitPrice: "600000",
},
{
pID: 794527,
Count: 1,
ParentID: 0,
Description: "",
productid: "1428542",
UnitPrice: "400000",
},
],
},
};
function mapOrders(orders) {
const rootOrders = orders.filter((order) => order.ParentID === 0);
return rootOrders.map((rootOrder) => {
const children = orders.filter((order) => order.ParentID === rootOrder.pID);
return {
...rootOrder,
children: children.length ? children : "",
};
});
}
// output
const mappedJsonVal = {
...JsonVal,
data: {
...JsonVal.data,
orders: mapOrders(JsonVal.data.orders),
},
};

format object array to grouped nested array object to generate antd tree table

I get the following array object from response and I need to implement antd tree table in my react application using this array object.
const students=[
{
"index": "5260574",
"studentName": "David Sinclair",
"marks": "177",
"gender": "M",
"resultId": 16,
"province": {
"code": "1",
"name": "Andregan"
},
"county": {
"name": "Silk",
"code": "21"
},
"city": {
"code": "234",
"name": "Meegan"
},
"rural": {
"code": "41",
"name": "Thumphil"
},
"canEvaluate": true
},
{
"index": "5259991",
"studentName": "KALURA GEETH DAMS",
"marks": "177",
"gender": "M",
"resultId": 17,
"province": {
"code": "1",
"name": "Andregan"
},
"county": {
"name": "Silk",
"code": "21"
},
"city": {
"code": "234",
"name": "Meegan"
},
"rural": {
"code": "41",
"name": "Thumphil"
},
"canEvaluate": true
},
{
"index": "5260534",
"studentName": "Mathew Jenkins",
"marks": "157",
"gender": "M",
"resultId": 21,
"province": {
"code": "1",
"name": "Andregan"
},
"county": {
"name": "Silk",
"code": "21"
},
"city": {
"code": "44",
"name": "Wrens"
},
"rural": {
"code": "49",
"name": "Cardin"
},
"canEvaluate": true
},
{
"index": "5260680",
"studentName": "MEDIW KENULA",
"marks": "177",
"gender": "M",
"resultId": 18,
"province": {
"code": "2",
"name": "Kiren"
},
"county": {
"name": "Georgia",
"code": "23"
},
"city": {
"code": "34",
"name": "Eden Hill"
},
"rural": {
"code": "99",
"name": "Southern Sea"
},
"canEvaluate": true
}
]
I want to group this response as province , county, city, rural wise as shown in below rendered antd tree table.
.
To get above tree table and get selected students, I need to format the response as below for the dataSource.
Expected output is as below.
const data = [
{
key: '1',
name: 'Andregan',
resultCount: 3,
resultIds: [16, 17, 21],
children: [
{
key: '21',
name: 'Silk',
resultCount: 2,
resultIds: [16, 17, 21],
children: [
{
key: '234',
name: 'Meegan',
resultCount: 2,
resultIds: [16, 17],
children: [
{
key: '41',
name: 'Thumphil',
resultCount: 2,
resultIds: [16, 17],
},
],
},
{
key: '44',
name: 'Wrens',
resultCount: 1,
resultIds: [21],
children: [
{
key: '49',
name: 'Cardin',
resultCount: 1,
resultIds: [21],
},
],
},
],
},
],
},
{
key: '2',
name: 'Kiren',
resultCount: 1,
resultIds: [18],
children: [
{
key: '23',
name: 'Georgia',
resultCount: 1,
resultIds: [18],
children: [
{
key: '34',
name: 'Eden Hill',
resultCount: 1,
resultIds: [18],
children: [
{
key: '99',
name: 'Southern Sea',
resultCount: 1,
resultIds: [18],
},
],
},
],
},
],
},
];
Can anyone suggest how to format response to above format to achieve this? Thank you.

How do rearrange the nested array

Below is my response which is getting from the MySQL database,
I want to rearrange childInterest like in the second desired output
"childInterest": [
{
"id": 1,
"parentInterestId": 3,
"created_at": "2022-03-04T07:30:31.851Z",
"updated_at": "2022-03-04T07:30:31.851Z",
"interest": {
"id": 6,
"interestId": 3,
"name": "collecting stamps",
"level": 2,
"createdAt": "2022-01-27T14:22:22.586Z",
"updatedAt": "2022-01-27T14:22:22.586Z",
"heirarchy": {
"id": 3,
"interestId": null,
"name": "hobbies",
"level": 1,
"createdAt": "2022-01-27T14:20:40.362Z",
"updatedAt": "2022-01-27T14:20:40.362Z"
}
}
},
{
"id": 2,
"parentInterestId": 3,
"created_at": "2022-03-04T07:30:31.863Z",
"updated_at": "2022-03-04T07:30:31.863Z",
"interest": {
"id": 7,
"interestId": 3,
"name": "chess play",
"level": 2,
"createdAt": "2022-01-27T14:22:48.734Z",
"updatedAt": "2022-01-27T14:22:48.734Z",
"heirarchy": {
"id": 3,
"interestId": null,
"name": "hobbies",
"level": 1,
"createdAt": "2022-01-27T14:20:40.362Z",
"updatedAt": "2022-01-27T14:20:40.362Z"
}
}
},
{
"id": 5,
"parentInterestId": 4,
"created_at": "2022-03-04T08:06:03.499Z",
"updated_at": "2022-03-04T08:06:03.499Z",
"interest": {
"id": 8,
"interestId": 4,
"name": "cricket",
"level": 2,
"createdAt": "2022-01-27T14:23:13.042Z",
"updatedAt": "2022-01-27T14:23:13.042Z",
"heirarchy": {
"id": 4,
"interestId": null,
"name": "outdoor",
"level": 1,
"createdAt": "2022-01-27T14:21:00.701Z",
"updatedAt": "2022-01-27T14:21:00.701Z"
}
}
},
{
"id": 6,
"parentInterestId": 4,
"created_at": "2022-03-04T08:06:03.519Z",
"updated_at": "2022-03-04T08:06:03.519Z",
"interest": {
"id": 9,
"interestId": 4,
"name": "coco",
"level": 2,
"createdAt": "2022-01-27T14:23:22.940Z",
"updatedAt": "2022-01-27T14:23:22.940Z",
"heirarchy": {
"id": 4,
"interestId": null,
"name": "outdoor",
"level": 1,
"createdAt": "2022-01-27T14:21:00.701Z",
"updatedAt": "2022-01-27T14:21:00.701Z"
}
}
}
],
And I want a response like below
"childInterest": [
{
title: "Hobbies",
titleitems: [
"Music",
"Video"
]
},
{
title: "Indoor",
titleItems: [
"Carrom",
"Table Tenis"
]
},
{
title: "Outdoor",
titleItems: [
"Cricket",
"Kabbadi"
]
}
]
}
],
In 1st array whatever names hobbies, outdoor is there should come under single object inside that array should hobbies names like music video etc.
You probably want to take a look at the map() function:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
My solution.
I won't be explaining steps by steps.
let childInterest = [
{
id: 1,
parentInterestId: 3,
created_at: "2022-03-04T07:30:31.851Z",
updated_at: "2022-03-04T07:30:31.851Z",
interest: {
id: 6,
interestId: 3,
name: "collecting stamps",
level: 2,
createdAt: "2022-01-27T14:22:22.586Z",
updatedAt: "2022-01-27T14:22:22.586Z",
heirarchy: {
id: 3,
interestId: null,
name: "hobbies",
level: 1,
createdAt: "2022-01-27T14:20:40.362Z",
updatedAt: "2022-01-27T14:20:40.362Z",
},
},
},
{
id: 2,
parentInterestId: 3,
created_at: "2022-03-04T07:30:31.863Z",
updated_at: "2022-03-04T07:30:31.863Z",
interest: {
id: 7,
interestId: 3,
name: "chess play",
level: 2,
createdAt: "2022-01-27T14:22:48.734Z",
updatedAt: "2022-01-27T14:22:48.734Z",
heirarchy: {
id: 3,
interestId: null,
name: "hobbies",
level: 1,
createdAt: "2022-01-27T14:20:40.362Z",
updatedAt: "2022-01-27T14:20:40.362Z",
},
},
},
{
id: 5,
parentInterestId: 4,
created_at: "2022-03-04T08:06:03.499Z",
updated_at: "2022-03-04T08:06:03.499Z",
interest: {
id: 8,
interestId: 4,
name: "cricket",
level: 2,
createdAt: "2022-01-27T14:23:13.042Z",
updatedAt: "2022-01-27T14:23:13.042Z",
heirarchy: {
id: 4,
interestId: null,
name: "outdoor",
level: 1,
createdAt: "2022-01-27T14:21:00.701Z",
updatedAt: "2022-01-27T14:21:00.701Z",
},
},
},
{
id: 6,
parentInterestId: 4,
created_at: "2022-03-04T08:06:03.519Z",
updated_at: "2022-03-04T08:06:03.519Z",
interest: {
id: 9,
interestId: 4,
name: "coco",
level: 2,
createdAt: "2022-01-27T14:23:22.940Z",
updatedAt: "2022-01-27T14:23:22.940Z",
heirarchy: {
id: 4,
interestId: null,
name: "outdoor",
level: 1,
createdAt: "2022-01-27T14:21:00.701Z",
updatedAt: "2022-01-27T14:21:00.701Z",
},
},
},
];
let newOne = Object.values(
childInterest.reduce(
(
acc,
{
interest: {
name: titleItems,
heirarchy: { name: title },
},
}
) => {
const key = [title];
acc[key] ||= { titleItems: [], title };
acc[key].titleItems.push(titleItems);
return acc;
},
{}
)
);
console.log(newOne);

How to group by array object and create array with value key javascript

How to group by array object from values to create new array object of values
Input
var promotions = [
{
"promotionID": 2,
"id": 1,
"qty": 1,
"productID": 1,
"product": "Nu Milk Tea 330ml",
"operator": null
}, {
"promotionID": 2,
"id": 2,
"qty": 2,
"productID": 2,
"product": "Product testing 2",
"operator": 1
}, {
"promotionID": 2,
"id": 3,
"qty": 3,
"productID": 3,
"product": "Golda Coffee Dolce Latte 200ml",
"operator": 2
}, {
"promotionID": 3,
"id": 4,
"qty": 8,
"productID": 54,
"product": "Masker Skrineer 3ply Motif 5pcs",
"operator": null
}, {
"promotionID": 3,
"id": 5,
"qty": 5,
"productID": 53,
"product": "Masker Skrineer 1ply Grey 5pcs",
"operator": 2
}, {
"promotionID": 3,
"id": 6,
"qty": 5,
"productID": 52,
"product": "Oronamin C Drink 120ml",
"operator": 1
}]
I want to make a new array of car objects that's grouped by promotionID
Expected Output
[
{
"promotionID": 2,
"data" : [
{
"promotionID": 2,
"id": 1,
"qty": 1,
"productID": 1,
"product": "Nu Milk Tea 330ml",
"operator": null
}, {
"promotionID": 2,
"id": 2,
"qty": 2,
"productID": 2,
"product": "Product testing 2",
"operator": 1
}, {
"promotionID": 2,
"id": 3,
"qty": 3,
"productID": 3,
"product": "Golda Coffee Dolce Latte 200ml",
"operator": 2
}
]
},
{
"promotionID": 3,
"data" : [
{
"promotionID": 3,
"id": 4,
"qty": 8,
"productID": 54,
"product": "Masker Skrineer 3ply Motif 5pcs",
"operator": null
}, {
"promotionID": 3,
"id": 5,
"qty": 5,
"productID": 53,
"product": "Masker Skrineer 1ply Grey 5pcs",
"operator": 2
}, {
"promotionID": 3,
"id": 6,
"qty": 5,
"productID": 52,
"product": "Oronamin C Drink 120ml",
"operator": 1
}
]
}
]
You could use reduce to group promotion by promotionId and then map through the entries to get the final result
var promotions = [
{
promotionID: 2,
id: 1,
qty: 1,
productID: 1,
product: 'Nu Milk Tea 330ml',
operator: null
},
{
promotionID: 2,
id: 2,
qty: 2,
productID: 2,
product: 'Product testing 2',
operator: 1
},
{
promotionID: 2,
id: 3,
qty: 3,
productID: 3,
product: 'Golda Coffee Dolce Latte 200ml',
operator: 2
},
{
promotionID: 3,
id: 4,
qty: 8,
productID: 54,
product: 'Masker Skrineer 3ply Motif 5pcs',
operator: null
},
{
promotionID: 3,
id: 5,
qty: 5,
productID: 53,
product: 'Masker Skrineer 1ply Grey 5pcs',
operator: 2
},
{
promotionID: 3,
id: 6,
qty: 5,
productID: 52,
product: 'Oronamin C Drink 120ml',
operator: 1
}
]
const res = Array.from(
promotions
.reduce((acc, promotion) => {
acc.set(
promotion.promotionID,
(acc.get(promotion.promotionID) || []).concat(promotion)
)
return acc
}, new Map)
.entries(),
([promotionId, data]) => ({ promotionId, data })
)
console.log(res)
This is my answer
var promotions = [
{
promotionID: 2,
id: 1,
qty: 1,
productID: 1,
product: "Nu Milk Tea 330ml",
operator: null,
},
{
promotionID: 2,
id: 2,
qty: 2,
productID: 2,
product: "Product testing 2",
operator: 1,
},
{
promotionID: 2,
id: 3,
qty: 3,
productID: 3,
product: "Golda Coffee Dolce Latte 200ml",
operator: 2,
},
{
promotionID: 3,
id: 4,
qty: 8,
productID: 54,
product: "Masker Skrineer 3ply Motif 5pcs",
operator: null,
},
{
promotionID: 3,
id: 5,
qty: 5,
productID: 53,
product: "Masker Skrineer 1ply Grey 5pcs",
operator: 2,
},
{
promotionID: 3,
id: 6,
qty: 5,
productID: 52,
product: "Oronamin C Drink 120ml",
operator: 1,
},
];
const objectPromotion = {};
for (index in promotions) {
const dataPromotion = objectPromotion[promotions[index].promotionID];
console.log("dataPromotion", dataPromotion)
if (dataPromotion) {
dataPromotion.push(promotions[index]);
} else {
objectPromotion[promotions[index].promotionID] = [promotions[index]];
}
}
const result = Object.keys(objectPromotion).map((item) => {
return {
promotionID: item,
data: objectPromotion[item],
};
});
console.log("result", result)
you should use lodash like this.
const _ = require('lodash');
let filteredPromotions=_.groupBy(promotions,'promotionID');
output:
{
'2': [
{
promotionID: 2,
id: 1,
qty: 1,
productID: 1,
product: 'Nu Milk Tea 330ml',
operator: null
},
{
promotionID: 2,
id: 2,
qty: 2,
productID: 2,
product: 'Product testing 2',
operator: 1
},
{
promotionID: 2,
id: 3,
qty: 3,
productID: 3,
product: 'Golda Coffee Dolce Latte 200ml',
operator: 2
}
],
'3': [
{
promotionID: 3,
id: 4,
qty: 8,
productID: 54,
product: 'Masker Skrineer 3ply Motif 5pcs',
operator: null
},
{
promotionID: 3,
id: 5,
qty: 5,
productID: 53,
product: 'Masker Skrineer 1ply Grey 5pcs',
operator: 2
},
{
promotionID: 3,
id: 6,
qty: 5,
productID: 52,
product: 'Oronamin C Drink 120ml',
operator: 1
}
]
}
to get exact output add those lines:
filteredPropomotions = Object.keys(filteredPropomotions).map((key, index)=> {return{promotionID:key,data:filteredPropomotions[key]}}
);
output:
[
{
"promotionID": 2,
"data" : [
{
"promotionID": 2,
"id": 1,
"qty": 1,
"productID": 1,
"product": "Nu Milk Tea 330ml",
"operator": null
}, {
"promotionID": 2,
"id": 2,
"qty": 2,
"productID": 2,
"product": "Product testing 2",
"operator": 1
}, {
"promotionID": 2,
"id": 3,
"qty": 3,
"productID": 3,
"product": "Golda Coffee Dolce Latte 200ml",
"operator": 2
}
]
},
{
"promotionID": 3,
"data" : [
{
"promotionID": 3,
"id": 4,
"qty": 8,
"productID": 54,
"product": "Masker Skrineer 3ply Motif 5pcs",
"operator": null
}, {
"promotionID": 3,
"id": 5,
"qty": 5,
"productID": 53,
"product": "Masker Skrineer 1ply Grey 5pcs",
"operator": 2
}, {
"promotionID": 3,
"id": 6,
"qty": 5,
"productID": 52,
"product": "Oronamin C Drink 120ml",
"operator": 1
}
]
}
]

Categories