Nested JSON parsing using JSONparse() - javascript

I cannot seem to extract the data I want from this (verified) JSON string coming from my home automation server (i.e. I cannot change the JSON return).
var myJson = '{ "Device_Num_6": { "states": [ { "id": 207, "service": "urn:upnp-org:serviceId:SwitchPower1", "variable": "Status", "value": "0" }, { "id": 208, "service": "urn:upnp-org:serviceId:Dimming1", "variable": "LoadLevelTarget", "value": "94" }, { "id": 209, "service": "urn:upnp-org:serviceId:Dimming1", "variable": "LoadLevelStatus", "value": "0" }, { "id": 210, "service": "urn:micasaverde-com:serviceId:HaDevice1", "variable": "Configured", "value": "1" }, { "id": 211, "service": "urn:micasaverde-com:serviceId:HaDevice1", "variable": "ModeSetting", "value": "1:;2:;3:;4:" }, { "id": 212, "service": "urn:micasaverde-com:serviceId:HaDevice1", "variable": "LastUpdate", "value": "1438440091" }, { "id": 213, "service": "urn:micasaverde-com:serviceId:HaDevice1", "variable": "FirstConfigured", "value": "1438440091" }, { "id": 214, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "Capabilities", "value": "209,140,0,4,17,1,L,R,RS,|38:1,39,112,114,115,134," }, { "id": 215, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "ManufacturerInfo", "value": "99,17495,12848" }, { "id": 216, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "VersionInfo", "value": "6,1,91,2,11" }, { "id": 217, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "NodeInfo", "value": "26,27,70,72,73,86," }, { "id": 218, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "Neighbors", "value": "1,2,3,4,6,7,8,9,11,12,13,14,15,16,17,21,25,26,27,31,32,33,34,35," }, { "id": 219, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "LastReset", "value": "0" }, { "id": 220, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "AssociationNum", "value": "0" }, { "id": 221, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "LastRouteUpdate", "value": "1468760253" }, { "id": 222, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "PollOk", "value": "43947" }, { "id": 223, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "LastNnu", "value": "1468735200,700" }, { "id": 224, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "LastArr", "value": "1468735200,700" }, { "id": 225, "service": "urn:micasaverde-com:serviceId:ZWaveDevice1", "variable": "PollNoReply", "value": "54" }, { "id": 226, "service": "urn:micasaverde-com:serviceId:ZWaveNetwork1", "variable": "ConsecutivePollFails", "value": "0" } ], "Jobs": [ ], "PendingJobs": 0, "tooltip": { "display": 0 }, "status": -1 }, "Using_2G": 0, "LoadTime": 1468766261, "DataVersion": 766262647, "UserData_DataVersion": 766261013, "TimeStamp": 1468779944, "lights_on": 3, "lights_off": 8, "doors_locked": 5, "doors_unlocked": 2, "sensors_tripped": 0, "sensors_not_tripped": 8, "failed_devices": 8, "visible_devices": 93, "partitions_active": 0, "partitions_notactive": 2, "alerts": [ { "PK_Device": 154, "Room": 10, "DeviceName": "Laundry Door Lock", "DeviceType": "urn:schemas-micasaverde-com:device:DoorLock:1", "PK_Alert": "4742240051", "LocalDate": "2016-07-17 10:38:22", "EventType": 4, "SourceType": 4, "Argument": 0, "Filesize": 0, "Severity": 2, "LocalTimestamp": 1468766302, "Code": "SL_BATTERYALARM", "NewValue": "0", "Description": "Laundry Door Lock", "Users": "", "Server_Storage": "", "Key": "", "Icon": "", "PK_Store": "0" }, { "PK_Device": 150, "Room": 23, "DeviceName": "Kids Bath Motion", "DeviceType": "urn:schemas-micasaverde-com:device:MotionSensor:1", "PK_Alert": "4742240181", "LocalDate": "2016-07-17 10:38:22", "EventType": 4, "SourceType": 4, "Argument": 0, "Filesize": 0, "Severity": 2, "LocalTimestamp": 1468766302, "Code": "SL_BATTERYALARM", "NewValue": "10", "Description": "Kids Bath Motion", "Users": "", "Server_Storage": "", "Key": "", "Icon": "", "PK_Store": "0" }, { "PK_Device": 101, "Room": 21, "DeviceName": "Cabana Door Lock", "DeviceType": "urn:schemas-micasaverde-com:device:DoorLock:1", "PK_Alert": "4742255081", "LocalDate": "2016-07-17 10:40:18", "EventType": 4, "SourceType": 4, "Argument": 0, "Filesize": 0, "Severity": 2, "LocalTimestamp": 1468766418, "Code": "DL_LOW_BATTERY", "NewValue": "1", "Description": "Cabana Door Lock", "Users": "", "Server_Storage": "", "Key": "", "Icon": "", "PK_Store": "0" }, { "PK_Device": 101, "Room": 21, "DeviceName": "Cabana Door Lock", "DeviceType": "urn:schemas-micasaverde-com:device:DoorLock:1", "PK_Alert": "4742255231", "LocalDate": "2016-07-17 10:40:18", "EventType": 4, "SourceType": 4, "Argument": 0, "Filesize": 0, "Severity": 3, "LocalTimestamp": 1468766418, "Code": "DL_LOCK_CHANGED", "NewValue": "3", "Description": "Cabana Door Lock", "Users": "", "Server_Storage": "", "Key": "", "Icon": "", "PK_Store": "0" }, { "PK_Device": 101, "Room": 21, "DeviceName": "Cabana Door Lock", "DeviceType": "urn:schemas-micasaverde-com:device:DoorLock:1", "PK_Alert": "4742255371", "LocalDate": "2016-07-17 10:40:18", "EventType": 4, "SourceType": 4, "Argument": 0, "Filesize": 0, "Severity": 3, "LocalTimestamp": 1468766418, "Code": "DL_CODE_CHANGED", "NewValue": "3", "Description": "Cabana Door Lock", "Users": "", "Server_Storage": "", "Key": "", "Icon": "", "PK_Store": "0" }, { "PK_Device": 101, "Room": 21, "DeviceName": "Cabana Door Lock", "DeviceType": "urn:schemas-micasaverde-com:device:DoorLock:1", "PK_Alert": "4742255471", "LocalDate": "2016-07-17 10:40:18", "EventType": 4, "SourceType": 4, "Argument": 0, "Filesize": 0, "Severity": 3, "LocalTimestamp": 1468766418, "Code": "DL_USERCODE", "NewValue": "UserID=\"1\" UserName=\"Main\"", "Description": "Cabana Door Lock", "Users": "", "Server_Storage": "", "Key": "", "Icon": "", "PK_Store": "0" }, { "PK_Device": 101, "Room": 21, "DeviceName": "Cabana Door Lock", "DeviceType": "urn:schemas-micasaverde-com:device:DoorLock:1", "PK_Alert": "4742255591", "LocalDate": "2016-07-17 10:40:18", "EventType": 4, "SourceType": 4, "Argument": 0, "Filesize": 0, "Severity": 3, "LocalTimestamp": 1468766418, "Code": "DL_LOCK_BUTTON", "NewValue": "1", "Description": "Cabana Door Lock", "Users": "", "Server_Storage": "", "Key": "", "Icon": "", "PK_Store": "0" }, { "PK_Device": 101, "Room": 21, "DeviceName": "Cabana Door Lock", "DeviceType": "urn:schemas-micasaverde-com:device:DoorLock:1", "PK_Alert": "4742255721", "LocalDate": "2016-07-17 10:40:18", "EventType": 4, "SourceType": 4, "Argument": 0, "Filesize": 0, "Severity": 1, "LocalTimestamp": 1468766418, "Code": "DL_LOCK_FAILURE", "NewValue": "1", "Description": "Cabana Door Lock", "Users": "", "Server_Storage": "", "Key": "", "Icon": "", "PK_Store": "0" }, { "PK_Device": 101, "Room": 21, "DeviceName": "Cabana Door Lock", "DeviceType": "urn:schemas-micasaverde-com:device:DoorLock:1", "PK_Alert": "4742255801", "LocalDate": "2016-07-17 10:40:18", "EventType": 4, "SourceType": 4, "Argument": 0, "Filesize": 0, "Severity": 1, "LocalTimestamp": 1468766418, "Code": "DL_PINFAILED", "NewValue": "1", "Description": "Cabana Door Lock", "Users": "", "Server_Storage": "", "Key": "", "Icon": "", "PK_Store": "0" }, { "PK_Device": 101, "Room": 21, "DeviceName": "Cabana Door Lock", "DeviceType": "urn:schemas-micasaverde-com:device:DoorLock:1", "PK_Alert": "4742255971", "LocalDate": "2016-07-17 10:40:18", "EventType": 4, "SourceType": 4, "Argument": 0, "Filesize": 0, "Severity": 2, "LocalTimestamp": 1468766418, "Code": "SL_BATTERYALARM", "NewValue": "0", "Description": "Cabana Door Lock", "Users": "", "Server_Storage": "", "Key": "", "Icon": "", "PK_Store": "0" } ], "ZWaveStatus": 1, "Mode": 1, "LocalTime": "2016-07-17 14:25:44 D" }';
var myObject = JSON.parse(myJson);
console.log(myObject.Device_Num_6.states[0].id); // should return 207

Your json has invalid format. Because of that you couldn't to parse it as JSON.parse method. Try to check it here http://jsonviewer.stack.hu/
UPD: issue with this string "NewValue": "UserID=\"1\" UserName=\"Main\"". You should make it as "NewValue": "UserID=\\"1\\" UserName=\\"Main\\""

Related

Unable to push children into parent based on parentId using JS

I am unable to push childEntries into parent based on parentId using JS. Actually I need to populate the data into a tree view table.
Here is the JSON I got from API
[{
"displayName": "",
"order": 1,
"status": "Active",
"description": "Application Top Section",
"createDate": "2020-01-01 12:00:00",
"parentId": 0,
"modifiedDate": "2020-01-01 12:00:00",
"id": 0,
"type": "section"
}, {
"displayName": "",
"order": 1,
"status": "Active",
"description": "Application Side Navigation Section",
"createDate": "2020-01-01 12:00:00",
"parentId": 1,
"modifiedDate": "2020-01-01 12:00:00",
"id": 1,
"type": "section"
}, {
"displayName": "Puente Dashboard",
"order": 1,
"status": "Active",
"description": "Application Dashboard",
"createDate": "2020-01-01 12:00:00",
"parentId": 1,
"modifiedDate": "2020-01-01 12:00:00",
"id": 2,
"type": "link"
}, {
"displayName": "Security",
"order": 2,
"status": "Active",
"description": "Security Management",
"createDate": "2020-01-01 12:00:00",
"parentId": 1,
"modifiedDate": "2020-01-01 12:00:00",
"id": 3,
"type": "link"
}, {
"displayName": "User",
"order": 1,
"status": "Active",
"description": "User Management",
"createDate": "2020-01-01 12:00:00",
"parentId": 3,
"modifiedDate": "2020-01-01 12:00:00",
"id": 4,
"type": "link"
}, {
"displayName": "Role",
"order": 2,
"status": "Active",
"description": "Role Management",
"createDate": "2020-01-01 12:00:00",
"parentId": 3,
"modifiedDate": "2020-01-01 12:00:00",
"id": 5,
"type": "link"
}, {
"displayName": "Widget",
"order": 3,
"status": "Active",
"description": "Widget Management",
"createDate": "2020-01-01 12:00:00",
"parentId": 3,
"modifiedDate": "2020-01-01 12:00:00",
"id": 6,
"type": "link"
}]
And expected JSON is
[{
"displayName": "",
"order": 1,
"status": "Active",
"description": "Application Top Section",
"createDate": "2020-01-01 12:00:00",
"parentId": 0,
"modifiedDate": "2020-01-01 12:00:00",
"id": 0,
"type": "section"
},
{
"displayName": "",
"order": 1,
"status": "Active",
"description": "Application Side Navigation Section",
"createDate": "2020-01-01 12:00:00",
"parentId": 1,
"modifiedDate": "2020-01-01 12:00:00",
"id": 1,
"type": "section",
"childEntries": [{
"displayName": "Puente Dashboard",
"order": 1,
"status": "Active",
"description": "Application Dashboard",
"createDate": "2020-01-01 12:00:00",
"parentId": 1,
"modifiedDate": "2020-01-01 12:00:00",
"id": 2,
"type": "link"
},
{
"displayName": "Security",
"order": 2,
"status": "Active",
"description": "Security Management",
"createDate": "2020-01-01 12:00:00",
"parentId": 1,
"modifiedDate": "2020-01-01 12:00:00",
"id": 3,
"type": "link",
"childEntries": [{
"displayName": "User",
"order": 1,
"status": "Active",
"description": "User Management",
"createDate": "2020-01-01 12:00:00",
"parentId": 3,
"modifiedDate": "2020-01-01 12:00:00",
"id": 4,
"type": "link"
},
{
"displayName": "Role",
"order": 2,
"status": "Active",
"description": "Role Management",
"createDate": "2020-01-01 12:00:00",
"parentId": 3,
"modifiedDate": "2020-01-01 12:00:00",
"id": 5,
"type": "link"
},
{
"displayName": "Widget",
"order": 3,
"status": "Active",
"description": "Widget Management",
"createDate": "2020-01-01 12:00:00",
"parentId": 3,
"modifiedDate": "2020-01-01 12:00:00",
"id": 6,
"type": "link"
}
]
}
]
}
]
Using below code only I am able to add "kind","childEntries" and "expanded". But ultimately I am not getting my expected JSON.
this.widgetsource.forEach(function (element) { if(element.parentId == 1 || element.parentId == 0 ){ element.kind = "dir"; element.expanded = false; element.childEntries = []; }
Here in this.widgetsource I have stored the raw JSON.
Could you please help me here.
Thanks in advance.
You can check if its a child when parent is not equal to itself, then find the parent and add in child list.
const parsedObject = JSON.parse(`[{ "displayName": "", "order": 1, "status": "Active", "description": "Application Top Section", "createDate": "2020-01-01 12:00:00", "parentId": 0, "modifiedDate": "2020-01-01 12:00:00", "id": 0, "type": "section" }, { "displayName": "", "order": 1, "status": "Active", "description": "Application Side Navigation Section", "createDate": "2020-01-01 12:00:00", "parentId": 1, "modifiedDate": "2020-01-01 12:00:00", "id": 1, "type": "section" }, { "displayName": "Puente Dashboard", "order": 1, "status": "Active", "description": "Application Dashboard", "createDate": "2020-01-01 12:00:00", "parentId": 1, "modifiedDate": "2020-01-01 12:00:00", "id": 2, "type": "link" }, { "displayName": "Security", "order": 2, "status": "Active", "description": "Security Management", "createDate": "2020-01-01 12:00:00", "parentId": 1, "modifiedDate": "2020-01-01 12:00:00", "id": 3, "type": "link" }, { "displayName": "User", "order": 1, "status": "Active", "description": "User Management", "createDate": "2020-01-01 12:00:00", "parentId": 3, "modifiedDate": "2020-01-01 12:00:00", "id": 4, "type": "link" }, { "displayName": "Role", "order": 2, "status": "Active", "description": "Role Management", "createDate": "2020-01-01 12:00:00", "parentId": 3, "modifiedDate": "2020-01-01 12:00:00", "id": 5, "type": "link" }, { "displayName": "Widget", "order": 3, "status": "Active", "description": "Widget Management", "createDate": "2020-01-01 12:00:00", "parentId": 3, "modifiedDate": "2020-01-01 12:00:00", "id": 6, "type": "link" }]`);
/**
* converting into a map for efficient access by ID id:Object.
*/
const idMap = parsedObject.reduce((accum,obj)=>{
accum[obj.id] = obj;
return accum;
},{});
const categorisedList = [];
parsedObject.forEach(obj=>{
if(obj.parentId === obj.id){
categorisedList.push(obj);
}else{
const parent = idMap[obj.parentId];
if(!parent.childEntries){
parent.childEntries=[];
}
parent.childEntries.push(obj);
}
});
console.log(categorisedList);
//If you need as JSON String.
const json = JSON.stringify(categorisedList);

How to create csv/excel file from response data reactjs

I have response data like below:
{
"Aerospace": [
{
"id": 62,
"branch": "Aerospace",
"classification": "Einstufungen 2",
"theme": "Produktentwicklung",
"branch_id": 1,
"classification_id": 1,
"projectno": "510",
"project_id": 52,
"theme_id": 6,
"projectname": "test 22",
"comment": "",
"customername": "test 12",
"lead1id": 14,
"lead1name": "DateTest",
"lead1percent": "1000000000.0",
"lead2id": 11,
"lead2name": "logintest",
"lead2percent": "2000000000.0",
"lead3id": 16,
"lead3name": "jomo",
"lead3percent": "3000000000.0",
"leadplan_probability": "100.0",
"leadplan_plan2": "200.0",
"leadplan_plan3": "300.0",
"leadplan_addcosts": "500.0",
"leadplan_fee": "400.0",
"lead_plans": [
{
"id": 15,
"lead_id": 62,
"addcosts": "500.0",
"fee": "400.0",
"plan2": "200.0",
"plan3": "300.0",
"probability": "100.0",
"year": 2020
},
{
"id": 18,
"lead_id": 62,
"addcosts": "600.0",
"fee": "500.0",
"plan2": "200.0",
"plan3": "400.0",
"probability": "100.0",
"year": 2020
},
{
"id": 19,
"lead_id": 62,
"addcosts": "500.0",
"fee": "400.0",
"plan2": "200.0",
"plan3": "300.0",
"probability": "100.0",
"year": 2020
}
],
"offers": [
{
"id": 53,
"lead_id": 62,
"addcosts": "400.0",
"addcostsinfo": "test5",
"days": null,
"decision": "test6",
"decisiondate": "2020-05-29",
"fee": "200.0",
"mail": "test",
"offerdate": "2020-05-22",
"paper": "test"
}
]
},
{
"leadplan_plan2": 200,
"leadplan_plan3": 300,
"leadplan_addcosts": 500,
"leadplan_fee": 400
}
],
"Electrical": [
{
"id": 65,
"branch": "Electrical",
"classification": "Einstufungen 2",
"theme": "Sales",
"branch_id": 2,
"classification_id": 1,
"projectno": "12345",
"project_id": 53,
"theme_id": 5,
"projectname": "project12345",
"comment": "test",
"customername": "test123",
"lead1id": 1,
"lead1name": "alpha1",
"lead1percent": "100.0",
"lead2id": 53,
"lead2name": "newuser",
"lead2percent": "200.0",
"lead3id": 51,
"lead3name": "testisbah",
"lead3percent": "300.0",
"leadplan_probability": "1000.0",
"leadplan_plan2": "200.0",
"leadplan_plan3": "300.0",
"leadplan_addcosts": "500.0",
"leadplan_fee": "4000.0",
"lead_plans": [
{
"id": 16,
"lead_id": 65,
"addcosts": "500.0",
"fee": "4000.0",
"plan2": "200.0",
"plan3": "300.0",
"probability": "1000.0",
"year": 2020
},
{
"id": 22,
"lead_id": 65,
"addcosts": "800.0",
"fee": "700.0",
"plan2": "500.0",
"plan3": "600.0",
"probability": "400.0",
"year": 2023
}
],
"offers": [
{
"id": 52,
"lead_id": 65,
"addcosts": "400.0",
"addcostsinfo": "4",
"days": null,
"decision": "4",
"decisiondate": "2020-05-20",
"fee": "400.0",
"mail": "4",
"offerdate": "2020-05-20",
"paper": "4"
},
{
"id": 55,
"lead_id": 65,
"addcosts": "0.0",
"addcostsinfo": "",
"days": null,
"decision": "",
"decisiondate": "2020-05-13",
"fee": "0.0",
"mail": "",
"offerdate": "2020-05-14",
"paper": ""
},
{
"id": 57,
"lead_id": 65,
"addcosts": "0.0",
"addcostsinfo": "",
"days": null,
"decision": "",
"decisiondate": "2020-05-31",
"fee": "0.0",
"mail": "",
"offerdate": "2020-05-09",
"paper": ""
},
{
"id": 56,
"lead_id": 65,
"addcosts": "0.0",
"addcostsinfo": "",
"days": null,
"decision": "",
"decisiondate": "2020-05-30",
"fee": "100.0",
"mail": "",
"offerdate": "2020-05-14",
"paper": ""
},
{
"id": 58,
"lead_id": 65,
"addcosts": "0.0",
"addcostsinfo": "",
"days": null,
"decision": "",
"decisiondate": "2020-05-06",
"fee": "0.0",
"mail": "",
"offerdate": "2020-05-14",
"paper": ""
}
]
},
{
"leadplan_plan2": 200,
"leadplan_plan3": 300,
"leadplan_addcosts": 500,
"leadplan_fee": 4000
}
]
}
And I would like to create an excel or CSV file from it but problem is that my data loads first and excel utility gives error related to null empty.
Is there any reasonable excel npm package from which I can export my data to csv/excel using button click in ReactJs?
If I would be able to export my complete table, that would be good.
The result excel view would be like:

How do I use Object.entries(...).forEach.every?

I'm filtering a list using a const queryModifier = {price: "lessThan", weight: "greaterThan"}
const queryKeys = keys: {
price: '1000',
weight: '1000'
}
const list = [
{
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-11-05 10:48:18",
"createdBy": "test#test.com",
"deliveryDate": "2019-11-08 10:46:37",
"driver": "",
"dropOff": "Kögel Trailer GmbH & Co. KG, Am Kögel-Werk, Burtenbach, Germany",
"height": 0,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "7e249529-d089-47bb-b0ad-470f850dd8cf",
"notes": "",
"orderId": "",
"pickUp": "Altenberge, Germany",
"price": 500,
"quantity": 1,
"trailer": "einzeln",
"vehicle": "Sattel",
"vehicleClass": "engl. Anschlüsse",
"vehicleId": "505123",
"vehicleReadyDate": "2019-11-05 10:47:57",
"weight": 0
},
{
"additionalPrices": 0,
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-10-14 16:32:12",
"createdBy": "test#test.com",
"deliveryDate": "2019-10-19 16:26:35",
"driver": "",
"dropOff": "Marville-Moutiers-Brûlé, France",
"height": 600,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "9b7f57fd-f95e-4038-b120-a0301fdf3f31",
"notes": "",
"orderId": "",
"pickUp": "Altenberge, Germany",
"price": 0,
"quantity": 1,
"trailer": "2er Pack",
"vehicle": "Anhänger",
"vehicleClass": "50",
"vehicleId": "123",
"vehicleReadyDate": "2019-10-16 16:31:26",
"weight": 12000
},
{
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-10-14 16:25:54",
"createdBy": "test#test.com",
"deliveryDate": "2019-10-19 16:24:13",
"driver": "",
"dropOff": "Vendenheim, France",
"height": 0,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "7e4f233f-695b-40eb-a0ca-fd78f3fa43cd",
"notes": "",
"orderId": "",
"pickUp": "Altenberge, Germany",
"price": 793,
"quantity": 1,
"trailer": "einzeln",
"vehicle": "Sattel",
"vehicleClass": "Standard",
"vehicleId": "3340731",
"vehicleReadyDate": "2019-10-15 16:24:58",
"weight": 0
},
{
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-09-26 18:32:18",
"createdBy": "test#test.com",
"deliveryDate": "2019-09-20 18:31:45",
"driver": "Michal Kucharski",
"dropOff": "Logroño, Spain",
"height": 0,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "388113f5-3927-4fe3-80d5-f2fcf1c7cedd",
"notes": "",
"orderId": "",
"pickUp": "16671 Butano Place, Fontana, CA, USA",
"price": 0,
"quantity": 1,
"trailer": "2er Pack",
"vehicle": "Sattel",
"vehicleClass": "Standard",
"vehicleId": "efgefg",
"vehicleReadyDate": "2019-09-27 18:32:08",
"weight": 0
},
{
"additionalPrices": 0,
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-09-06 22:57:55",
"createdBy": "test#test.com",
"deliveryDate": "2019-09-07 22:57:03",
"driver": "Eugeniusz Galinski",
"dropOff": "12345 Lamplight Village Avenue, Austin, TX, USA",
"height": 32,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "ac3cf14e-b43b-45e4-9168-ad4997b6415d",
"notes": "Nzube I am adding notes here",
"pickUp": "16671 Butano Place, Fontana, CA, USA",
"price": 100,
"quantity": 3,
"trailer": "einzeln",
"vehicle": "Sattel",
"vehicleClass": "Mega",
"vehicleId": "123",
"vehicleReadyDate": "2019-09-26 22:57:36",
"weight": 12
},
{
"additionalPrices": 0,
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-09-06 22:46:25",
"createdBy": "test#test.com",
"deliveryDate": "2019-09-06 22:45:45",
"driver": "Michal Kucharski",
"dropOff": "QEW, Niagara Falls, ON, Canada",
"height": 67,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "d0ec3b82-2279-4d11-8e35-a9307713ae5a",
"notes": "This is coming along",
"pickUp": "Avenida Callao 1234, Buenos Aires, Argentina",
"price": 100,
"quantity": 1,
"trailer": "2er Pack",
"vehicle": "Sattel",
"vehicleClass": "Mega",
"vehicleId": "123",
"vehicleReadyDate": "2019-09-25 22:45:57",
"weight": 12
},
{
"additionalPrices": 0,
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-09-04 18:06:18",
"createdBy": "test#test.com",
"deliveryDate": "2019-09-05 18:05:51",
"driver": "Chibuzo ilogu",
"dropOff": "Asda Park Royal Superstore, Western Road, London, UK",
"height": 453,
"isPreparingTrailer": true,
"isSmsSent": true,
"isTrailerReady": true,
"key": "96ee9410-4d70-4bbc-8016-5d7c9e5ecec1",
"notes": "ewriupoi ",
"pickUp": "Avenida Juan Bautista Alberdi 1233, Buenos Aires, Argentina",
"price": 234,
"quantity": 1,
"trailer": "3er Pack",
"vehicle": "Anhänger",
"vehicleClass": "50",
"vehicleId": "543",
"vehicleReadyDate": "2019-09-21 18:06:03",
"weight": 453
},
{
"additionalPrices": 0,
"companyName": "Test Company",
"completeDate": "2019-09-04 18:02:27",
"createdAt": "2019-09-04 18:01:30",
"createdBy": "test#test.com",
"deliveryDate": "2019-09-04 18:01:09",
"driver": "Chibuzo ilogu",
"dropOff": "La Cabaña 123, Las Condes, Chile",
"height": 123,
"isPreparingTrailer": false,
"isSmsSent": true,
"isTrailerReady": true,
"key": "bcd50f43-0644-49a7-8bdc-009a4572341b",
"notes": "qewqe",
"pickUp": "La Cabaña 123, Las Condes, Chile",
"price": 123,
"trailer": "einzeln",
"vehicle": "Sattel",
"vehicleClass": "Standard",
"vehicleId": "123",
"vehicleReadyDate": "2019-10-02 18:01:19",
"weight": 123
}
];
export const handleFilterModifier = (value, compareValue, modifier) => {
if (modifier === 'lessThan') {
return value > compareValue;
}
if (modifier === 'equals') {
return value === compareValue;
}
if (modifier === 'greaterThan') {
return value < compareValue;
}
return null;
};
const resultList = list.filter(
item => Object.entries(queryModifiers).every(([filterKey, filterVal]) => {
const compareValue = item[filterKey];
const value = Object.values(queryKeys);
const result = handleFilterModifier(
parseFloat(value),
compareValue,
filterVal
);
return result;
})
);
This operation fails when either of the queryModifiers has "equals" in its value pair. Anything else returns the actual true result. I don't understand why it fails on "equals".
I feel like there should be a forEach somewhere but I don't know where to put it and maybe there's something wrong with the code. I need help figuring it out.
I think you should replace this
const queryKeys = keys: {
price: '1000',
weight: '1000'
}
with
const queryKeys = {
price: '1000',
weight: '1000'
}
and replace this
const value = Object.values(queryKeys);
with
const value = queryKeys[filterKey];

Tree massiv get all the links of the elements

I have and object literal that is essentially a tree that does not have a fixed number of levels.
How can I search for a tree for a particular node, and then return this node with connection parameters when it is found in a spectacular javascript manner?
var data = [{
title: 'topNode',
id: 1,
children: [{
title: 'node1',
id: 2,
children: [{
title: 'randomNode_1',
id: 3,
},
{
title: 'node2',
id: 4,
children: [{
title: 'randomNode_2',
id: 5,
children: [{
title: 'node2',
id: 1111,
children: [{
title: 'randomNode_3',
id: 1232,
}]
}]
}]
}
]
}]
}];
tree build   what should be done
tree built from api
serializeGoals: state => type => {
const _goals = JSON.parse(JSON.stringify(state.goals))
return {
title: 'Стратегия',
expand: true,
forst: true,
children: _goals.filter((item) => {
if (item.type !== null && type.some(typeID => typeID === item.type.id)) {
item.shadow = true
} else if (item.type !== null && type.length > 0) {
item.shadow = false
item.type.color = '#ECECEC'
}
item.children = _goals.filter((children) => {
if (children.parent_id === item.id) {
if (children.type !== null && type.some(typeID => typeID === children.type.id)) {
children.shadow = true
} else if (children.type !== null && type.length > 0) {
children.type.color = '#ECECEC'
children.shadow = false
}
return true
}
return false
})
return item.parent_id === null
})
}
}
Get api server JSON array
parent_id - element parent
[{
"id": 5,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 5",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 7,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 7",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 9,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 9",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 11,
"parent_id": null,
"responsible_id": null,
"type_id": 3,
"title": "Тестовая цель - 11",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 3, "title": "Процессы", "color": "#13ce66"}
}, {
"id": 13,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 13",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 15,
"parent_id": null,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 15",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 17,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 17",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 19,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 19",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 20,
"parent_id": 11,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 20",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 21,
"parent_id": null,
"responsible_id": null,
"type_id": 3,
"title": "Тестовая цель - 21",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 3, "title": "Процессы", "color": "#13ce66"}
}, {
"id": 23,
"parent_id": null,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 23",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 25,
"parent_id": null,
"responsible_id": null,
"type_id": 1,
"title": "Тестовая цель - 25",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 1, "title": "Финансы", "color": "#ffc82c"}
}, {
"id": 26,
"parent_id": 15,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 26",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 27,
"parent_id": null,
"responsible_id": null,
"type_id": 1,
"title": "Тестовая цель - 27",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 1, "title": "Финансы", "color": "#ffc82c"}
}, {
"id": 28,
"parent_id": 7,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 28",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 29,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 29",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 31,
"parent_id": null,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 31",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 33,
"parent_id": null,
"responsible_id": null,
"type_id": 3,
"title": "Тестовая цель - 33",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 3, "title": "Процессы", "color": "#13ce66"}
}, {
"id": 34,
"parent_id": 31,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 34",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 35,
"parent_id": null,
"responsible_id": null,
"type_id": 1,
"title": "Тестовая цель - 35",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 1, "title": "Финансы", "color": "#ffc82c"}
}, {
"id": 36,
"parent_id": 34,
"responsible_id": null,
"type_id": 1,
"title": "Тестовая цель - 36",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 1, "title": "Финансы", "color": "#ffc82c"}
}, {
"id": 37,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 37",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 39,
"parent_id": null,
"responsible_id": null,
"type_id": 3,
"title": "Тестовая цель - 39",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 3, "title": "Процессы", "color": "#13ce66"}
}, {
"id": 40,
"parent_id": 34,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 40",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}]
I would use a recursive function to iterate through the tree and return the node when the expected one is found (I assumed the title is the search criteria) or return null if none was found.
In example :
var data = [
{
title: 'topNode',
id: 1,
children: [
{
title: 'node1',
id: 2,
children: [
{
title: 'randomNode_1',
id: 3,
},
{
title: 'node2',
id: 4,
children: [
{
title: 'randomNode_2',
id: 5,
children: [
{
title: 'node2',
id: 1111,
children: [
{
title: 'randomNode_3',
id: 1232,
}]
}]
}]
}]
}]
}];
function FindSpecificData(node, titleToFind)
{
let newNode = null;
for (let i = 0; i < node.length; ++i)
{
if (node[i].title == titleToFind)
newNode = node[i];
else if ('children' in node[i])
newNode = FindSpecificData(node[i].children, titleToFind);
if (newNode != null)
return (newNode);
}
return (null);
}
console.log(FindSpecificData(data, 'randomNode_2'));
console.log(FindSpecificData(data, 'randomNode_1'));
console.log(FindSpecificData(data, 'missing node'));
Try this simple method to find out your node
function getNode(node) {
if (node.title === 'randomNode_3') {
return true;
} else if (!node.children && !node.id) {
return false;
} else {
if (node.children) {
for (var i = 0; i < node.children.length; i++) {
getNode(node.children[i])
}
}
}
}
getNode(data[0])

JSON hierarchy, how to get an element?

$("#loginsubmit").live("click", function() {
var request = new Request();
request.service = "http://88.80.223.163:8080/lumiin-service/lumiin/control/eventmap/1" ;
RequestHandler(request,displayData);
});
function displayData(data) {
//alert(data.contents.ownerId);
var cont = data.contents;
$("#mono").html(''+cont.id);
}
I am using the above code snippet to get the id, which works fine...How do I get the firstName?
Object:
{
"id": 1,
"eventId": {
"URL": "web",
"location": "web",
"url": "web",
"id": 54,
"type": {
"name": "Meeting",
"id": 1,
"isDeleted": "N",
"description": "Meeting"
},
"abteilung": {
"id": 165,
"code": "web123",
"isDeleted": null,
"domainId": 0,
"parentId": 164,
"description": null
},
"eventName": "Tanvi",
"place": "web",
"startDate": 1335830400000,
"endDate": 1337558400000,
"prospectId": null,
"person": "web",
"isDeleted": "N",
"countryId": {
"name": "CH",
"id": 1,
"isDeleted": "N",
"description": "Switzerland"
},
"followUp": null,
"budget": 0,
"description": "web"
},
"ownerId": {
"id": 67,
"firstName": "web123",
"localOrg": null,
"industryTypeId": null,
"memeber": false,
"profileType": "Manager",
"emailId": "web123",
"abteilung": {
"id": 165,
"code": "web123",
"isDeleted": null,
"domainId": 0,
"parentId": 164,
"description": null
},
"userCode": "web123",
"authenticatedAccess": -1,
"uuid": null,
"lastName": "web123",
"isDeleted": "N",
"passWord": "web123",
"fullName": "web123,web123",
"status": 0
},
"prospectId": {
"id": 303,
"firstName": "web123",
"emailId": "web123#gg.com",
"ownerId": {
"id": 67,
"firstName": "web123",
"localOrg": null,
"industryTypeId": null,
"memeber": false,
"profileType": "Manager",
"emailId": "web123",
"abteilung": {
"id": 165,
"code": "web123",
"isDeleted": null,
"domainId": 0,
"parentId": 164,
"description": null
},
"userCode": "web123",
"authenticatedAccess": -1,
"uuid": null,
"lastName": "web123",
"isDeleted": "N",
"passWord": "web123",
"fullName": "web123,web123",
"status": 0
},
"abteilung": {
"id": 165,
"code": "web123",
"isDeleted": null,
"domainId": 0,
"parentId": 164,
"description": null
},
"startDate": 1335744000000,
"endDate": 1337904000000,
"statusId": {
"name": "New",
"id": 1,
"isDeleted": "N",
"description": "New"
},
"company": "company",
"lastName": "web123",
"isDeleted": "N",
"yearofBirth": null,
"annualRevenue": 0,
"nationalityId": {
"name": "CH",
"id": 1,
"isDeleted": "N",
"description": "Switzerland"
},
"titleId": {
"name": "Mr",
"id": 1,
"isDeleted": "N",
"description": "Mr"
},
"maritalstatusId": null,
"residencyId": null,
"languageId": null,
"priorityId": {
"name": "Low",
"id": 1,
"isDeleted": "N",
"description": "Low"
},
"genderId": {
"name": "Male",
"id": 1,
"isDeleted": "N",
"description": "Male"
}
},
"isDeleted": "N"
}
The firstName is a property of the prospectId object you can read it in this way:
var firstName = data.prospectId.firstName;
var lastName = data.prospectId.lastName;
Fiddle

Categories