Issue
I want to create a new array with the 'itemDetails' from the following array. In this array inside every 'poDetails', 'itemDetails' is listed, I need to create a new array with all these 'itemDetails'(if it is repeated also). I tried some methods using map, foreach, but whiling pushing to new array its not getting as expected
expecting result =
[ {"cic": 36200097,"description": "TEST ITEM","qty": 312},
{"cic": 36200038,"description": "TEST ITEM","qty": 156},
{"cic": 36200097,"description": "TEST ITEM","qty": 468},
{"cic": 36200038,"description": "TEST ITEM","qty": 156},
{"cic": 36200097,"description": "TEST ITEM","qty": 468}]
obj= {
"toolName": "WHSE Case",
"poDetails": [
{
"po": 678676,
"itemDetails": [
{
"cic": 36200097,
"description": "TEST ITEM",
"qty": 312,
},
{
"cic": 36200038,
"description": "TEST ITEM",
"qty": 156,
}
]
},
{
"po": 680510,
"itemDetails": [
{
"cic": 36200097,
"description": "TEST ITEM",
"qty": 468,
},
{
"cic": 36200038,
"description": "TEST ITEM",
"qty": 156,
}
]
},
{
"po": 682110,
"itemDetails": [
{
"cic": 36200097,
"description": "TEST ITEM",
"qty": 468,
}
]
},
]
}
this.gridRows = array.poDetails;
this.gridRows.forEach(row => {
if (row.itemDetails) {
this.formatedData.push(row.itemDetails);
}
});
this is waht I tried and not getting the result as expected.
Solution
If you tried already, you clearly didn't try hard enough. A simple map + flat is all it takes.
const data = {
toolName: 'WHSE Case',
poDetails: [
{
po: 678676,
itemDetails: [
{ cic: 36200097, description: 'TEST ITEM', qty: 312, },
{ cic: 36200038, description: 'TEST ITEM', qty: 156, },
],
},
{
po: 680510,
itemDetails: [
{ cic: 36200097, description: 'TEST ITEM', qty: 468, },
{ cic: 36200038, description: 'TEST ITEM', qty: 156, },
],
},
{
po: 682110,
itemDetails: [
{ cic: 36200097, description: 'TEST ITEM', qty: 468, },
],
},
],
};
const grouped = data.poDetails.map(({ itemDetails }) => itemDetails).flat(1);
console.log(grouped);
Answered By - MGX
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.