javascript for repeats in object [closed] - javascript
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
hey guys I have a problem :)
the result is wrong.I am filtering for personnal codes and dates. every personal has an array for it. You can see it from dateArr. in dateArr you can see 2 different arrays. these are two different personal actually. In vm.resReport there is dates for these personels I want to took the values of personnel that in vm.resREport and personnel code of it equal to dateArr's personnel. And put these values to dateArr
for(var item in vm.resReport){
for(var k=0;k < Object.keys(vm.dateArr).length;k++){
for(var days in vm.dateArr[k].Days){
if(vm.resReport[item].mDate==vm.dateArr[k].Days[days].FullDate){
for(var per in vm.resReport[item].Details){
if(vm.dateArr[k].PersonnelCode==vm.resReport[item].Details[per].PersonnelCode){
console.log(vm.resReport[item].Details[per].PersonnelFirstName);
vm.dateArr[k].Days[days].PersonnelCode=[];
vm.dateArr[k].Days[days].PersonnelCode[vm.dateArr[k].PersonnelCode]=vm.resReport[item].Details[per].PersonnelCode;
}
}
}
}
}
}
my problem is, in the second repeat. The for circle works actually but it repeats all together for whatever the k. but it needs to finilise the first k element than the other. but it just repeats all together.
How can I solve this? Thank you :) Cheers!
Edit: Here my Object
{
0:{Days:Array[30],PersonnelCode:"PRS_001" PersonnelFirstName:"Güvenc" PersonnelLastName:"Vural"},
1:{Days:Array[30],PersonnelCode:"PRS_111" PersonnelFirstName:"Timur" PersonnelLastName:"Hacizade"}
}
here my resReport Object;
[
{
"Total": 1,
"mDate": "2016-02-15",
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 1
}
]
},
{
"Total": 1,
"mDate": "2016-03-08",
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 1
}
]
},
{
"Total": 1,
"mDate": "2016-04-08",
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 1
}
]
},
{
"Total": 3,
"mDate": "2016-04-09",
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 2
},
{
"PersonnelFirstName": "Timur",
"PersonnelLastName": "Hacizade",
"PersonnelCode": "PRS_111",
"Total": 1
}
]
},
{
"Total": 1,
"mDate": "2016-04-13",
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 1
}
]
},
{
"Total": 1,
"mDate": "2016-04-14",
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 1
}
]
}
]
and here my dateArr Object
[
{
"PersonnelCode": "PRS_001",
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"Days": [
{
"Day": "01",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-01",
"ID": 92,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "02",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-02",
"ID": 93,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "03",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-03",
"ID": 94,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "04",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-04",
"ID": 95,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "05",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-05",
"ID": 96,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "06",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-06",
"ID": 97,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "07",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-07",
"ID": 98,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "08",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-08",
"ID": 99,
"StatusName": 1,
"Popover": false,
"Update": false,
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 1
}
]
},
{
"Day": "09",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-09",
"ID": 100,
"StatusName": 3,
"Popover": false,
"Update": false,
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 2
},
{
"PersonnelFirstName": "Timur",
"PersonnelLastName": "Hacizade",
"PersonnelCode": "PRS_111",
"Total": 1
}
]
},
{
"Day": 10,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-10",
"ID": 101,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 11,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-11",
"ID": 102,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 12,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-12",
"ID": 103,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 13,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-13",
"ID": 104,
"StatusName": 1,
"Popover": false,
"Update": false,
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 1
}
]
},
{
"Day": 14,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-14",
"ID": 105,
"StatusName": 1,
"Popover": false,
"Update": false,
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 1
}
]
},
{
"Day": 15,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-15",
"ID": 106,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 16,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-16",
"ID": 107,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 17,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-17",
"ID": 108,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 18,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-18",
"ID": 109,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 19,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-19",
"ID": 110,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 20,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-20",
"ID": 111,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 21,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-21",
"ID": 112,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 22,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-22",
"ID": 113,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 23,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-23",
"ID": 114,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 24,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-24",
"ID": 115,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 25,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-25",
"ID": 116,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 26,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-26",
"ID": 117,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 27,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-27",
"ID": 118,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 28,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-28",
"ID": 119,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 29,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-29",
"ID": 120,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 30,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-30",
"ID": 121,
"StatusName": "NA",
"Popover": false,
"Update": false
}
],
"Update": false
},
{
"PersonnelCode": "PRS_111",
"PersonnelFirstName": "Timur",
"PersonnelLastName": "Hacizade",
"Days": [
{
"Day": "01",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-01",
"ID": 92,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "02",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-02",
"ID": 93,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "03",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-03",
"ID": 94,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "04",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-04",
"ID": 95,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "05",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-05",
"ID": 96,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "06",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-06",
"ID": 97,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "07",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-07",
"ID": 98,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": "08",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-08",
"ID": 99,
"StatusName": 1,
"Popover": false,
"Update": false,
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 1
}
]
},
{
"Day": "09",
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-09",
"ID": 100,
"StatusName": 3,
"Popover": false,
"Update": false,
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 2
},
{
"PersonnelFirstName": "Timur",
"PersonnelLastName": "Hacizade",
"PersonnelCode": "PRS_111",
"Total": 1
}
]
},
{
"Day": 10,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-10",
"ID": 101,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 11,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-11",
"ID": 102,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 12,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-12",
"ID": 103,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 13,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-13",
"ID": 104,
"StatusName": 1,
"Popover": false,
"Update": false,
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 1
}
]
},
{
"Day": 14,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-14",
"ID": 105,
"StatusName": 1,
"Popover": false,
"Update": false,
"Details": [
{
"PersonnelFirstName": "Güvenc",
"PersonnelLastName": "Vural",
"PersonnelCode": "PRS_001",
"Total": 1
}
]
},
{
"Day": 15,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-15",
"ID": 106,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 16,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-16",
"ID": 107,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 17,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-17",
"ID": 108,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 18,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-18",
"ID": 109,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 19,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-19",
"ID": 110,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 20,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-20",
"ID": 111,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 21,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-21",
"ID": 112,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 22,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-22",
"ID": 113,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 23,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-23",
"ID": 114,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 24,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-24",
"ID": 115,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 25,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-25",
"ID": 116,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 26,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-26",
"ID": 117,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 27,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-27",
"ID": 118,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 28,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-28",
"ID": 119,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 29,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-29",
"ID": 120,
"StatusName": "NA",
"Popover": false,
"Update": false
},
{
"Day": 30,
"Cls": "",
"ClsStatus": "",
"FullDate": "2016-04-30",
"ID": 121,
"StatusName": "NA",
"Popover": false,
"Update": false
}
],
"Update": false
}
]
Please check the solution and tell me if it solved the problem.
for(var item in vm.resReport){
var report = vm.resReport[item];
for(var k=0;k < Object.keys(vm.dateArr).length;k++){
var dateInfo = vm.dateArr[k];
var days = dateInfo.Days;
for(var day in days){
var currDay = days[day];
if(report.mDate==currDay.FullDate){
for(var per in report.Details){
var detail = report.Details[per];
if(dateInfo.PersonnelCode==detail.PersonnelCode){
console.log(detail.PersonnelFirstName);
currDay.PersonnelCode=[];
currDay.PersonnelCode[dateInfo.PersonnelCode]=detail.PersonnelCode;
}
}
}
}
}
}
If you want all the k = 0 done before all the k = 1, and so on, you need to invert the nesting of the loops.
for(var k=0;k < Object.keys(vm.dateArr).length;k++){
for(var item in vm.resReport){
...
}
}
Related
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 to implement a search by object by value if each of them is invested in years and months
I made a website and I just can't manage to search by multidimensional array / object (I don’t know what the difference is). Before adding the year and month, I accessed them directly by identifier array[id][field] how to implement search by nested structures in a multidimensional array I tried to pre-define the year and month with additional fields in the interface. and it looks creepy and incomprehensible. { "2018": { "Aug": { "1": { "id": 1, "appeal_date": "2018-08-24", "city_of_residence": "aaa", "patient_name": "John", "patient_age": 62, "coordinator": "aaa", "source": "aaa", "birth_date": "1956-06-30", "contact_person_name": "", "contact_person_role": "", "contact_person_phones": [ "", "" ], "diagnosis": "aasasd", "status": "9", "departure_date": "2016-02-18", "arrival_date": "2020-01-23", "patient_phones": [ "", "" ], "editable": true } } }, "2019": { "Oct": { "65": { "id": 65, "appeal_date": "2019-10-18", "city_of_residence": "asfsac", "patient_name": "asvsas", "patient_age": 62, "coordinator": "", "source": "asfasfa", "birth_date": "1956-06-30", "contact_person_name": "", "contact_person_role": "", "contact_person_phones": "", "diagnosis": "assdbcx", "status": "1", "departure_date": "", "arrival_date": "", "patient_phones": "", "editable": true } }, "Jun": { "64": { "id": 64, "appeal_date": "2019-06-04", "city_of_residence": "afsfsa", "patient_name": "asvac", "patient_age": 62, "coordinator": "", "source": "agwdawdawd", "birth_date": "1956-06-30", "contact_person_name": "", "contact_person_role": "", "contact_person_phones": "", "diagnosis": "agsags", "status": "1", "departure_date": "", "arrival_date": "", "patient_phones": "", "editable": true } } } } all i need is to update some fields in the right object patients [2019] ['Oct'] [65] ['diagnosis'] = 'aaaaa' something like this, but in a certain month the patient may not be right I would like something like this: patients.nestingSearchByKey (65) ['diagnosis'] = 'aaaaa' their identifiers are unique and not repeated I apologize for my English any ideas please
You can use Object.keys to get an array of keys in the object. var pts = { "2018": { "Aug": { "1": { "id": 1, "appeal_date": "2018-08-24", "city_of_residence": "aaa", "patient_name": "John", "patient_age": 62, "coordinator": "aaa", "source": "aaa", "birth_date": "1956-06-30", "contact_person_name": "", "contact_person_role": "", "contact_person_phones": [ "", "" ], "diagnosis": "aasasd", "status": "9", "departure_date": "2016-02-18", "arrival_date": "2020-01-23", "patient_phones": [ "", "" ], "editable": true } } }, "2019": { "Oct": { "65": { "id": 65, "appeal_date": "2019-10-18", "city_of_residence": "asfsac", "patient_name": "asvsas", "patient_age": 62, "coordinator": "", "source": "asfasfa", "birth_date": "1956-06-30", "contact_person_name": "", "contact_person_role": "", "contact_person_phones": "", "diagnosis": "assdbcx", "status": "1", "departure_date": "", "arrival_date": "", "patient_phones": "", "editable": true } }, "Jun": { "64": { "id": 64, "appeal_date": "2019-06-04", "city_of_residence": "afsfsa", "patient_name": "asvac", "patient_age": 62, "coordinator": "", "source": "agwdawdawd", "birth_date": "1956-06-30", "contact_person_name": "", "contact_person_role": "", "contact_person_phones": "", "diagnosis": "agsags", "status": "1", "departure_date": "", "arrival_date": "", "patient_phones": "", "editable": true } } } } function search(patients, id) { var patient; const years = Object.keys(patients); years.forEach(year => { const months = Object.keys(patients[year]); months.forEach(month => { if (patients[year][month][id]) { patient = patients[year][month][id]; } }); }); return patient; } const p = search(pts, 64); if (p) { p['status'] = 0; console.log(pts); } else { console.log('Not find patient with id 64'); }
If I understood you currectly and you're keeping the current object structure, you just need to check for patient existence: var patient = patients[2019]['Oct'][65]; if (patient) { patient['diagnosis'] = 'aaaaa'; }
Node Red smart Thermostat
I need help with increments in Node Red (on Raspberry Pi). I am trying to make a smart thermostat with a LCD touchscreen and an app for remote access (ex. coming home on a cold winter day and want to start heating your apartment). Some suggestions on how to connect to the Raspberry Pi over the Internet (not local network) with an android and a link on a tutorial or example would be much appreciated. P.S. I am doing this for a friend and I promised him some good results. Sorry for messy code... => cleaned it up in the edit I have tried everything I could think of to make my code work as I am not very experienced with NodeRed nor JS. Global variables May just do the trick... I haven't figured it out yet... Another barrier I have encountered is the ability to access and interact with the code outside the local network. [ { "id": "9f63513b.e227a", "type": "ui_slider", "z": "f216bae9.be3b68", "name": "", "label": "", "group": "b344cd03.40f29", "order": 2, "width": "0", "height": "0", "passthru": true, "topic": "slide", "min": "10", "max": "45", "step": 1, "x": 650, "y": 140, "wires": [ [ "4e35591.c5466a8", "ef681f9b.17845" ] ] }, { "id": "4e35591.c5466a8", "type": "delay", "z": "f216bae9.be3b68", "name": "Buffer", "pauseType": "queue", "timeout": "5", "timeoutUnits": "seconds", "rate": "1", "nbRateUnits": "1", "rateUnits": "second", "randomFirst": "1", "randomLast": "5", "randomUnits": "seconds", "drop": true, "x": 810, "y": 60, "wires": [ [ "eec05221.3091e" ] ] }, { "id": "82848cf2.b33d1", "type": "function", "z": "f216bae9.be3b68", "name": "delete payload", "func": "msg.buton = msg.payload;\ndelete msg.payload;\nmsg.incalzire=msg.buton;\nreturn msg;", "outputs": 1, "noerr": 0, "x": 480, "y": 240, "wires": [ [ "9f63513b.e227a" ] ] }, { "id": "ef681f9b.17845", "type": "ui_gauge", "z": "f216bae9.be3b68", "name": "", "group": "b344cd03.40f29", "order": 1, "width": 0, "height": 0, "gtype": "gage", "title": "", "label": "", "format": "{{value}}", "min": "0", "max": "60", "colors": [ "#00b500", "#e6e600", "#ca3838" ], "seg1": "", "seg2": "", "x": 810, "y": 220, "wires": [] }, { "id": "eec05221.3091e", "type": "function", "z": "f216bae9.be3b68", "name": "delete payload", "func": "msg.tempslide=msg.payload;\ndelete msg.payload;\n\nreturn msg;", "outputs": 1, "noerr": 0, "x": 1000, "y": 60, "wires": [ [ "f5853672.5fe148" ] ] }, { "id": "6b57c137.8c136", "type": "function", "z": "f216bae9.be3b68", "name": "Switch", "func": "if(msg.payload===0){\n msg.payload=1;\n}\nelse if (msg.payload==1){\n msg.payload=0;\n}\nreturn msg;", "outputs": 1, "noerr": 0, "x": 310, "y": 460, "wires": [ [ "82848cf2.b33d1" ] ] }, { "id": "f5853672.5fe148", "type": "function", "z": "f216bae9.be3b68", "name": "working mess", "func": "if (msg.incalzire===0||msg.payload===0){\n\nif (msg.temp<=msg.tempslide){\n msg.payload=0;\n}\n\nelse if (msg.temp>msg.tempslide){\n msg.payload=1;\n}\n\nelse{\n msg.payload=1;}\n \n}\n\n\nelse{\n msg.payload=1;\n \n}\n\n\n\n\nreturn msg;", "outputs": 1, "noerr": 0, "x": 1058, "y": 140, "wires": [ [ "3f6977e5.b44158", "ee3a7840.808ad8", "7726596c.b74228" ] ] }, { "id": "d34a3234.79712", "type": "ui_switch", "z": "f216bae9.be3b68", "name": "", "label": "Override Window", "group": "b344cd03.40f29", "order": 4, "width": 0, "height": 0, "passthru": true, "decouple": "false", "topic": "", "style": "", "onvalue": "1", "onvalueType": "num", "onicon": "", "oncolor": "", "offvalue": "0", "offvalueType": "num", "officon": "", "offcolor": "", "x": 250, "y": 420, "wires": [ [ "6b57c137.8c136" ] ] }, { "id": "3f6977e5.b44158", "type": "debug", "z": "f216bae9.be3b68", "name": "", "active": false, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "x": 1293, "y": 140, "wires": [] }, { "id": "ee3a7840.808ad8", "type": "function", "z": "f216bae9.be3b68", "name": "Message", "func": "if(msg.payload===0){\n msg.payload='PORNITA'; //ON\n}\nelse if (msg.payload==1){\n msg.payload='OPRITA'; //OFF\n}\nreturn msg;", "outputs": 1, "noerr": 0, "x": 1183, "y": 340, "wires": [ [ "eaeac5fb.c27728" ] ] }, { "id": "7726596c.b74228", "type": "rpi-gpio out", "z": "f216bae9.be3b68", "name": "Rel1", "pin": "22", "set": "", "level": "0", "freq": "", "out": "out", "x": 1233, "y": 220, "wires": [] }, { "id": "c0784b3b.c1fcb8", "type": "function", "z": "f216bae9.be3b68", "name": "Switch", "func": "if(msg.payload===0){\n msg.payload=1;\n}\nelse if (msg.payload==1){\n msg.payload=0;\n}\nreturn msg;", "outputs": 1, "noerr": 0, "x": 250, "y": 380, "wires": [ [ "d34a3234.79712" ] ] }, { "id": "eaeac5fb.c27728", "type": "ui_text", "z": "f216bae9.be3b68", "group": "b344cd03.40f29", "order": 3, "width": 0, "height": 0, "name": "", "label": "Incalzirea este", "format": "{{msg.payload}}", "layout": "row-right", "x": 1220, "y": 380, "wires": [] }, { "id": "248c4825.00c018", "type": "rpi-gpio in", "z": "f216bae9.be3b68", "name": "", "pin": "40", "intype": "tri", "debounce": "25", "read": false, "x": 90, "y": 300, "wires": [ [ "c0784b3b.c1fcb8" ] ] }, { "id": "afb8009a.49841", "type": "function", "z": "f216bae9.be3b68", "name": "delete payload", "func": "//nu umbla la asta\nmsg.temp=msg.payload;\ndelete msg.payload;\nreturn msg;", "outputs": 1, "noerr": 0, "x": 320, "y": 140, "wires": [ [ "c0784b3b.c1fcb8" ] ] }, { "id": "c696dbbc.58ee98", "type": "rpi-dht22", "z": "f216bae9.be3b68", "name": "", "topic": "Interior", "dht": "22", "pintype": "1", "pin": "29", "x": 140, "y": 180, "wires": [ [ "afb8009a.49841", "33ec6fb6.fda7f" ] ] }, { "id": "33ec6fb6.fda7f", "type": "ui_gauge", "z": "f216bae9.be3b68", "name": "", "group": "d2bfb34b.1c943", "order": 1, "width": 0, "height": 0, "gtype": "gage", "title": "", "label": "° C", "format": "{{value}}", "min": 0, "max": "60", "colors": [ "#00b500", "#e6e600", "#ca3838" ], "seg1": "", "seg2": "", "x": 150, "y": 220, "wires": [] }, { "id": "d3a6bff.b99a44", "type": "inject", "z": "f216bae9.be3b68", "name": "", "topic": "", "payload": "", "payloadType": "date", "repeat": "50", "crontab": "", "once": false, "onceDelay": 0.1, "x": 110, "y": 120, "wires": [ [ "c696dbbc.58ee98" ] ] }, { "id": "b344cd03.40f29", "type": "ui_group", "z": "", "name": "Reglarea Incalzirii", "tab": "af80de11.bfd15", "order": 2, "disp": true, "width": "6", "collapse": true }, { "id": "d2bfb34b.1c943", "type": "ui_group", "z": "", "name": "Temperatura Curenta", "tab": "af80de11.bfd15", "order": 1, "disp": true, "width": "6", "collapse": true }, { "id": "af80de11.bfd15", "type": "ui_tab", "z": "", "name": "Sergiu", "icon": "fa-thermometer-full", "order": 4 } ] The old thermostat was basically an automated relay switch. I have to make it smart and my best bet is a raspberry pi. The increment part can replace the slider(I have Backups) and has to be triggered by a button in the Dashboard UI. Everything except of what I mentioned works corectly. Thank you for your help!! (or at least your time)
Nested JSON parsing using JSONparse()
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\\""
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