gpt4 book ai didi

javascript - 无法弄清楚我做错了什么。未处理的拒绝(类型错误): Cannot read property 'inspection' of undefined

转载 作者:行者123 更新时间:2023-12-04 07:35:31 26 4
gpt4 key购买 nike

所以我有一个从 Api 调用数据的事件处理程序。
现在我正在编写一个函数来格式化传入的数据。

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
});
},
}}
传入的数据如下所示:
之前的这个例子来自同一家餐厅,但也可能是不同的餐厅,以关键的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"
}
如果它们是同一家餐厅,我想将同一对象下同一家餐厅的不同违规行为与新的违规 key 结合起来。就像下面的代码。
{
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"
}
这是我写的函数
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;
};
我可以从控制台中的 console.log() 看到第一次迭代。但是在我收到类型错误之后
enter image description here
有人可以帮助我了解我做错了什么。
谢谢你的时间。

最佳答案

我想我会使用 reduce 来验证你的逻辑.它设置了一个 violation每个对象的数组以保持一致性,并大大减少了代码库。如果您正在优化违规数组之外的对象,那么添加到其中也很容易。

let odata = [{
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"
}
]


let newdata = odata.reduce((final, obj) => {
let vobj = {
date: obj.inspection_date,
type: obj.inspection_type,
violation_code: obj.violation_code,
violation_description: obj.violation_description
}
if (final[obj.camis]) final[obj.camis].violation.push(vobj)
else final[obj.camis] = { ...obj,
violation: [vobj]
}
return final;
}, {})

console.log(Object.values(newdata))

关于javascript - 无法弄清楚我做错了什么。未处理的拒绝(类型错误): Cannot read property 'inspection' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67764688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com