so I have a eventhandler that calls for data from an Api. Now Im writing a function that formats the incoming data.
eventHandlers={{
dragend : async (e)=>{
const lat = e.target._latlng.lat;
const lng = e.target._latlng.lng;
const result = await callSodaApi(lat, lng);
const data = consolidate(result.data); <<<<<<<<<<<<<<<-
console.log(data);
setMapConfig({
markerCoord : [lat, lng],
queryData : result.data
});
},
}}
the incoming data looks like this : this example before is from the same restaurant, but could also be a different restaurant differentiate by the key camis.
{
action: "D"
boro: "Manhattan"
building: "625"
camis: "40363298"
critical_flag: "G"
cuisine_description: "American"
dba: "CAFE METRO"
grade: "A"
grade_date: "2016-05-13T00:00:00.000"
inspection_date: "2016-05-13T00:00:00.000"
inspection_type: "Cycle Inspection / Initial Inspection"
latitude: "40.756185423077"
longitude: "-73.990564733785"
record_date: "2021-05-29T06:03:39.000"
score: "4"
street: "8 AVENUE"
violation_code: "10E"
violation_description: "Accurate thermometer not provided in refrigerated or hot holding equipment."
zipcode: "10018"
},
{
action: "U"
boro: "Manhattan"
building: "625"
camis: "40363298"
critical_flag: "C"
cuisine_description: "American"
dba: "CAFE METRO"
grade: "A"
grade_date: "2017-10-10T00:00:00.000"
inspection_date: "2017-10-10T00:00:00.000"
inspection_type: "Cycle Inspection / Re-inspection"
latitude: "40.756185423077"
longitude: "-73.990564733785"
phone: "2127149342"
record_date: "2021-05-29T06:03:39.000"
score: "12"
street: "8 AVENUE"
violation_code: "04H"
violation_description: "Raw, cooked or prepared food is adulterated, contaminated, cross-contaminated, or not discarded in accordance with HACCP plan."
zipcode: "10018"
}
if they are the same restaurant, I want to combine the different violations from the same restaurant under the same object with new violation key. Like the code below.
{
action: "U"
boro: "Manhattan"
building: "625"
camis: "40363298"
critical_flag: "C"
cuisine_description: "American"
dba: "CAFE METRO"
grade: "A"
grade_date: "2017-10-10T00:00:00.000"
latitude: "40.756185423077"
longitude: "-73.990564733785"
phone: "2127149342"
record_date: "2021-05-29T06:03:39.000"
score: "12"
street: "8 AVENUE"
violation: [{
date: "2016-05-13T00:00:00.000"
type: "Cycle Inspection / Initial Inspection"
violation_code: "04H"
violation_description: "Raw, cooked or prepared food is adulterated, contaminated, cross-contaminated, or not discarded in accordance with HACCP plan."/
},{
date: "2017-10-10T00:00:00.000"
type: "Cycle Inspection / Re-inspection"
violation_code: "04H"
violation_description: "Raw, cooked or prepared food is adulterated, contaminated, cross-contaminated, or not discarded in accordance with HACCP plan."
}]
zipcode: "10018"
}
this is the function I wrote,
const consolidate = (dataArray) => {
let a = 0;
let nxt = 1;
let holdingArray = [];
while ( nxt < dataArray.length-1 ) {
let newObj;
// if same restaurant
if ( dataArray[a].camis === dataArray[nxt].camis ) {
// when this is the first iteration of restaruant
newObj = nxt - a === 1
? {
camis: dataArray[a].camis,
dba: dataArray[a].dba,
cuisine: dataArray[a].cuisine_description,
building: dataArray[a].building,
street: dataArray[a].street,
boro: dataArray[a].boro,
zipcode: dataArray[a].zipcode,
grade : dataArray[a].grade || dataArray[nxt].grade,
latitude: dataArray[a].latitude,
longitude: dataArray[a].longitude,
inspection:
[ {
date: dataArray[a].inspection_date,
type: dataArray[a].inspection_type,
violation_code: dataArray[a].violation_code,
violation_description: dataArray[a].violation_description
}, {
date: dataArray[nxt].inspection_date,
type: dataArray[nxt].inspection_type,
violation_code: dataArray[nxt].violation_code,
violation_description: dataArray[nxt].violation_description
}]
}
: {
camis: dataArray[a].camis,
dba: dataArray[a].dba,
cuisine: dataArray[a].cuisine_description,
building: dataArray[a].building,
street: dataArray[a].street,
boro: dataArray[a].boro,
zipcode: dataArray[a].zipcode,
grade : dataArray[a].grade || dataArray[nxt].grade,
latitude: dataArray[a].latitude,
longitude: dataArray[a].longitude,
inspection: [...newObj.inspection , {
date: dataArray[nxt].inspection_date,
type: dataArray[nxt].inspection_type,
violation_code: dataArray[nxt].violation_code,
violation_description: dataArray[nxt].violation_description
}]
};
console.log(newObj) <<<<<<<<<<
nxt ++;
} else {
holdingArray.push(newObj);
a = nxt;
nxt = nxt + 1;
};
};
return holdingArray;
};
I'm able to see the very first iteration from the console.log() in the console. But right after I would get a type error
Can someone help me understand what I'm doing wrong please. And Thank you for your time.
