Shape Tabular Data into Hierachy - javascript

I'd like to reshape this tabular data
[{
"orderid": 1,
"grandtotal": 100.00,
"detailid": 11,
"description": "apple"
},
{
"orderid": 1,
"grandtotal": 100.00,
"detailid": 12,
"description": "orange"
},
{
"orderid": 2,
"grandtotal": 0.00
},
{
"orderid": 3,
"grandtotal": 300.00,
"detailid": 31,
"description": "tea"
},
{
"orderid": 3,
"grandtotal": 300.00,
"detailid": 32,
"description": "coffee"
}]
which is like this
into master/slave hierarchy:
[{
"orderid": 1,
"grandtotal": 100.00,
"details": [{
"detailid": 11,
"description": "apple"
},
{
"detailid": 12,
"description": "orange"
}
]
},
{
"orderid": 2,
"grandtotal": 0.00,
"details": []
},
{
"orderid": 3,
"grandtotal": 300.00,
"details": [{
"detailid": 31,
"description": "tea"
},
{
"detailid": 32,
"description": "coffee"
}
]
}
]
How to do it rather than conventional for loop?

const reshape = (inputArray) => {
const reducedArray = inputArray.reduce((resultedObject, { orderid, grandtotal, detailid, description }) => {
let detail = null;
if (detailid !== undefined || description !== undefined) detail = {detailid, description};
if (orderid in resultedObject && detail) {
resultedObject[orderid].details.push(detail);
}
if (!(orderid in resultedObject)) {
resultedObject[orderid] = {orderid, grandtotal, details: []};
detail && resultedObject[orderid].details.push(detail);
}
return resultedObject;
}, {});
return Object.values(reducedArray);
}
reshape(data);

Related

Filter complex array in react native?

I have array .
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}]
I tried following the code to filter it. and no output show
arr.map((item,idx) => (
console.log(item.data.games.gamename)
)
))
I want to print all game name eg.
cricket
football
videogames
volleyball
We can use flatMap() to do it
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}]
// multiple flatMap chain invocation seems ugly,waiting for more elegant solution
let result = arr.flatMap(a => a.data).flatMap(a => a.games).flatMap(a => a.gamename)
console.log(result)
Data is a array and so is games:
const arr = [
{
status: "success",
data: [
{
name: "user1",
games: [
{
id: 1,
gamename: "cricket",
},
{
id: 2,
gamename: "football",
},
],
},
{
name: "user1",
games: [
{
id: 1,
gamename: "videogames",
},
{
id: 2,
gamename: "volleyball",
},
],
},
],
},
];
arr.map((item) => {
item.data.map((item) => {
item.games.map((item) => {
console.log(item.gamename);
});
});
});
Try out this code, it will return only game names, you can change the join if don't need comma (,)
Output :
"cricket,football,videogames,volleyball"
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}];
console.log(JSON.stringify(arr.filter(e=>e.status=="success").map(e=>e.data.map(f=>f.games.map(g=>g.gamename)).join(",")).join(",")));

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),
},
};

Loop through object javascript

This is my response body. I want to select only Collaborator's "supportNotifications" key-value set to true, how can I do that?
{
"status": true,
"date": "2022-04-06T16:33:13.630Z",
"data": {
"collaborators": [
{
"name": "Paul",
"lastCheckin": "2022-03-31T19:54:50.584Z",
"sales": 1,
"createdAt": "2022-03-30T14:47:48.478Z",
"id": "62446d4ab7f4da001e8f40dc",
"supportNotification": true,
"collaboratorId": 1
},
{
"name": "John",
"lastCheckin": null,
"sales": 0,
"createdAt": "2022-03-01",
"id": "62446ea4b7f4da001e8f40df",
"supportNotification": false,
"collaboratorId": 6
}
],
"count": 2
}
}
let response = {
"status": true,
"date": "2022-04-06T16:33:13.630Z",
"data": {
"collaborators": [
{
"name": "Paul",
"lastCheckin": "2022-03-31T19:54:50.584Z",
"sales": 1,
"createdAt": "2022-03-30T14:47:48.478Z",
"id": "62446d4ab7f4da001e8f40dc",
"supportNotification": true,
"collaboratorId": 1
},
{
"name": "John",
"lastCheckin": null,
"sales": 0,
"createdAt": "2022-03-01",
"id": "62446ea4b7f4da001e8f40df",
"supportNotification": false,
"collaboratorId": 6
}
],
"count": 2
}
};
const collaborators = response.data.collaborators.filter(c => c.supportNotification);
This will give you each collaborator object that has supportNotification = true. Is this the result you are trying to select?
var body = {
status: true,
date: "2022-04-06T16:33:13.630Z",
data: {
collaborators: [
{
name: "Paul",
lastCheckin: "2022-03-31T19:54:50.584Z",
sales: 1,
createdAt: "2022-03-30T14:47:48.478Z",
id: "62446d4ab7f4da001e8f40dc",
supportNotification: true,
collaboratorId: 1,
},
{
name: "John",
lastCheckin: null,
sales: 0,
createdAt: "2022-03-01",
id: "62446ea4b7f4da001e8f40df",
supportNotification: false,
collaboratorId: 6,
},
],
count: 2,
},
};
var result = [];
body.data.collaborators.forEach((item) => {
if (item.supportNotification === true) {
result.push(item);
}
});
console.log(result);

Nested Array Filter/Mapping

Probably a basic question, but I've been blocked for a day now on this.
I am trying to get the correct map/filter from the following array:
[{
"id": 1,
"description": "Electric",
"subUtilityTypes": [{
"id": 5,
"description": "Grid"
},
{
"id": 6,
"description": "Solar"
}
]
},
{
"id": 2,
"description": "Gas",
"subUtilityTypes": [{
"id": 7,
"description": "Heating Oil"
},
{
"id": 8,
"description": "Natural Gas"
},
{
"id": 11,
"description": "Propane"
}
]
}
]
I want to get the id and description inside all subUtilityTypes.
This is what I've been trying:
this.options = arr1.map((parent) => {
return {
id: parent.id,
name: parent.subUtilityTypes.flatMap((child) => {
return child.description;
})
};
});
My problem with what I am trying is that instead of creating separate objects I am getting parent id and list of child names like this:
[{
"id": 1,
"name": [
"Grid",
"Solar"
]
},
{
"id": 2,
"name": [
"Heating Oil",
"Natural Gas",
"Propane"
]
}
]
Expected result should look like this:
[{
"id": 5,
"name": [
"Grid"
]
},
{
"id": 6,
"name": [
"Solar"
]
},
{
"id": 7,
"name": [
"Heating Oil"
]
}
]
Firstly use flatMap to get subUtilityTypes, then map entries:
const input = [{id:1,description:"Electric",subUtilityTypes:[{id:5,description:"Grid"},{id:6,description:"Solar"}]},{id:2,description:"Gas",subUtilityTypes:[{id:7,description:"Heating Oil"},{id:8,description:"Natural Gas"},{id:11,description:"Propane"}]}];
const res = input.flatMap(e => e.subUtilityTypes)
.map(e => ({ id: e.id, name: [e.description] }))
console.log(res)
.as-console-wrapper { max-height: 100% !important; top: 0; } /* ignore this */
Map only arr1 by returning only subUtilityTypes and then map it to get the desired result :
arr1.flatMap(item=>item.subUtilityTypes)
.map(item=>({id:item.id,name:[item.description]}))
let arr1=[
{
"id": 1,
"description": "Electric",
"subUtilityTypes": [
{
"id": 5,
"description": "Grid"
},
{
"id": 6,
"description": "Solar"
}
]
},
{
"id": 2,
"description": "Gas",
"subUtilityTypes": [
{
"id": 7,
"description": "Heating Oil"
},
{
"id": 8,
"description": "Natural Gas"
},
{
"id": 11,
"description": "Propane"
}
]
}
]
let options=arr1.flatMap(item=>item.subUtilityTypes).map(item=>({id:item.id,name:[item.description]}))
console.log(options)
You can also use reduce to achieve the same result.
arr.reduce((acc, curr) => [...acc,...curr.subUtilityTypes.map((o) => ({ id: o.id, name: [o.description] })),],[])
const arr = [
{
id: 1,
description: "Electric",
subUtilityTypes: [
{
id: 5,
description: "Grid",
},
{
id: 6,
description: "Solar",
},
],
},
{
id: 2,
description: "Gas",
subUtilityTypes: [
{
id: 7,
description: "Heating Oil",
},
{
id: 8,
description: "Natural Gas",
},
{
id: 11,
description: "Propane",
},
],
},
];
const result = arr.reduce((acc, curr) => [...acc,...curr.subUtilityTypes.map((o) => ({ id: o.id, name: [o.description] })),],[]);
console.log(result);

Group elements of array by sub string JavaScript

I have a list of elements like this
[{
"id": 1,
"id_team": "1.",
"name": "Name",
}, {
"id": 2,
"id_team": "2.",
"name": "Name",
}, {
"id": 3,
"id_team": "3.",
"name": "Name",
}, {
"id": 4,
"id_team": "4.",
"name": "Name",
}, {
"id": 5,
"id_team": "5.",
"name": "Name",
}, {
"id": 6,
"id_team": "2.1.",
"name": "Name",
}, {
"id": 7,
"id_team": "6.",
"name": "Name",
}, {
"id": 8,
"id_team": "1.1.",
"name": "Name",
}, {
"id": 9,
"id_team": "1.2.",
"name": "Name",
}, {
"id": 10,
"id_team": "1.3.",
"name": "Name",
}, {
"id": 11,
"id_team": "1.4.",
"name": "Name",
}]
a shown in the example, i have some string in the column id_team that are inside other string in the same column in the array
For example, the main team would be 1. then 1.1. and 1.2. and so on would be part of 1.
so the result must be like this
[{
"id": 1,
"id_team": "1.",
"name": "Name",
"subteams": [{
"id": 8,
"id_team": "1.1.",
"name": "Name",
}, {
"id": 9,
"id_team": "1.2.",
"name": "Name",
}, {
"id": 10,
"id_team": "1.3.",
"name": "Name",
}, {
"id": 11,
"id_team": "1.4.",
"name": "Name",
},],
}, {
"id": 2,
"id_team": "2.",
"name": "Name",
"subitems": [{
"id": 6,
"id_team": "2.1.",
"name": "Name",
},]
}, {
"id": 3,
"id_team": "3.",
"name": "Name",
"subitems": [],
}, {
"id": 4,
"id_team": "4.",
"name": "Name",
"subitems": [],
}, {
"id": 5,
"id_team": "5.",
"name": "Name",
"subitems": [],
}, {
"id": 7,
"id_team": "6.",
"name": "Name",
"subitems": [],
},]
it is doable? i use lodash if it would make it easier.
EDIT
I have something like this
teams.filter(item => item.id_team.length <= size).map((item) => {
const subteams = teams.filter((team) =>
team.id_team.indexOf(item.id_team) === 0 && item.id_team !== team.id_team);
console.log(subteams);
return {
...item,
subteams,
};
}) || [];
this kinda work, as it will insert in the sub teams array, but the problem is that this must work for many levels, take this example
[{
"id": 1,
"id_team": "1.",
"name": "Team Name",
"subteams": [{
"id": 8,
"id_team": "1.1.",
"name": "Team Name",
}, {
"id": 9,
"id_team": "1.2.",
"name": "Team Name",
}, {
"id": 10,
"id_team": "1.3.",
"name": "Team Name",
}, {
"id": 11,
"id_team": "1.4.",
"name": "Team Name",
}, {
"id": 12,
"id_team": "1.1.1",
"name": "Team Name",
}]
}, {
"id": 2,
"id_team": "2.",
"name": "Team Name",
"subteams": [{
"id": 6,
"id_team": "2.1.",
"name": "Team Name",
}]
}, {
"id": 3,
"id_team": "3.",
"name": "Team Name",
"subteams": []
}, {
"id": 4,
"id_team": "4.",
"name": "Team Name",
"subteams": []
}, {
"id": 5,
"id_team": "5.",
"name": "Team Name",
"subteams": []
}, {
"id": 7,
"id_team": "6.",
"name": "Team Name",
"subteams": []
}]
in this example there is a team with id_team = 1.1.1., how can i add that team to a sub team of the team with id_team 1.1. with the same logic, i tried to use the same code recursively but it didnt work.
const items = data.reduce((acc, item, index) => {
const id_team = item.id_team.split('.')[0] + '.';
const current = acc.find(record => record.id_team === id_team);
if(current) current.subteams.push(item);
else acc.push({
id: index + 1,
id_team,
subteams: [item]
});
return acc;
}, []);
let data = [{
"id": 1,
"id_team": "1.",
"name": "Name",
}, {
"id": 2,
"id_team": "2.",
"name": "Name",
}, {
"id": 3,
"id_team": "3.",
"name": "Name",
}, {
"id": 4,
"id_team": "4.",
"name": "Name",
}, {
"id": 5,
"id_team": "1.2.5.",
"name": "Name",
}, {
"id": 6,
"id_team": "2.1.",
"name": "Name",
}, {
"id": 7,
"id_team": "6.",
"name": "Name",
}, {
"id": 8,
"id_team": "1.1.",
"name": "Name",
}, {
"id": 9,
"id_team": "1.2.",
"name": "Name",
}, {
"id": 10,
"id_team": "1.3.",
"name": "Name",
}, {
"id": 11,
"id_team": "1.4.",
"name": "Name",
}];
data = data.map(node=>{
let t = node.id_team.split('.').filter(n=>n);
let parent = t.splice(0,t.length-1).join('.');
return {
...node,
parent: !!parent ? parent+'.' : null
}
});
let tree = (items, id = null) =>
items
.filter(item => item.parent === id)
.map(item => ({ ...item, subTeams: tree(items, item.id_team) }));
console.log(JSON.stringify(tree(data),null,2));
From the code of the example, i just checked if the subteams array has data, if it does, i use an auxiliar with the recursive function and assigned it to the subteams.
getSubTeams(teams, size) {
const filtered = teams.filter(item => item.id_team.length <= size);
return filtered.map((item) => {
let subteams = teams.filter((team) => team.id_team.indexOf(item.id_team) === 0 && item.id_team !== team.id_team);
if (subteams.length > 0) {
let aux = this.getSubTeams(subteams, size + 2);
subteams = [
...aux,
];
}
return {
...item,
subteams,
};
}) || [];
},

Categories