Javascript 我想不出我做错了什么。未处理的拒绝(TypeError):无法读取属性';检查';未定义的

Javascript 我想不出我做错了什么。未处理的拒绝(TypeError):无法读取属性';检查';未定义的,javascript,algorithm,object,Javascript,Algorithm,Object,所以我有一个eventhandler,它从Api调用数据。 现在我正在编写一个函数来格式化传入的数据 eventHandlers={{ dragend : async (e)=>{ const lat = e.target._latlng.lat; const lng = e.target._latlng.lng; const result = await callSodaApi(l

所以我有一个eventhandler,它从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
              });
            },
          }}
如果他们是同一家餐厅,我想将同一家餐厅在同一个对象下的不同违规行为与新的违规密钥结合起来。就像下面的代码

{
  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;
};
const consolidation=(数据数组)=>{
设a=0;
设nxt=1;
设holdingaray=[];
while(nxtconsole.log(newObj)错误消息中指出了故障线路。您正在尝试在
newObj
上访问
inspection

为了更好地理解,我在下面的代码中复制了您的错误:

let newObj;
newObj={
检查:newObj.inspection//newObj此时仍未定义

}
我想我应该尝试一下您的逻辑,使用
reduce
。它为每个对象设置一个
冲突
数组,以确保一致性,并大大减少代码库。如果您在冲突数组之外细化对象,那么也很容易添加到这个数组中

让odata=[{
行动:“D”,
波罗:“曼哈顿”,
建筑物:“625”,
卡米斯:“40363298”,
关键标志:“G”,
美食描述:“美国”,
dba:“地铁咖啡厅”,
成绩:“A”,
等级日期:“2016-05-13T00:00:00.000”,
检查日期:“2016-05-13T00:00:00.000”,
检验类型:“周期检验/初始检验”,
纬度:“40.756185423077”,
经度:“-73.990564733785”,
记录日期:“2021-05-29T06:03:39.000”,
得分:“4”,
街道:“8大道”,
违规代码:“10E”,
违反说明:“冷藏或热保存设备中未提供准确的温度计。”,
zipcode:“10018”
},
{
行动:“U”,
波罗:“曼哈顿”,
建筑物:“625”,
卡米斯:“40363298”,
临界_标志:“C”,
美食描述:“美国”,
dba:“地铁咖啡厅”,
成绩:“A”,
等级日期:“2017-10-10T00:00:00.000”,
检查日期:“2017-10-10T00:00:00.000”,
检验类型:“周期检验/复检”,
纬度:“40.756185423077”,
经度:“-73.990564733785”,
电话:"2127149342",
记录日期:“2021-05-29T06:03:39.000”,
分数:“12”,
街道:“8大道”,
违规代码:“04H”,
违反说明:“生的、熟的或准备好的食品根据HACCP计划掺假、污染、交叉污染或未丢弃。”,
zipcode:“10018”
}
]
让newdata=odata.reduce((final,obj)=>{
设vobj={
日期:obj.inspection_日期,
类型:obj.inspection_类型,
违规代码:obj.违规代码,
违规描述:obj.违规描述
}
if(final[obj.camis])final[obj.camis].invalition.push(vobj)
else final[obj.camis]={…obj,
违反:[vobj]
}
返回最终结果;
}, {})

console.log(Object.values(newdata))
您应该以文本而不是图像的形式发布错误,以提高网站搜索引擎的可读性和索引,谢谢您的提示!我以后一定会这样做。
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;
};