Javascript 如何在筛选数组时忽略某些对象

Javascript 如何在筛选数组时忽略某些对象,javascript,arrays,undefined,Javascript,Arrays,Undefined,我在下面的代码中有一个returnedDocs数组,通过让returnedDocs=result.application.modules.moduleItem缩短;作为API的响应,它的结构非常复杂。有些嵌套对象是数组,有些值放置在结构的几层深处 我使用filter方法只获取获得特定值的元素 let toFilterSix = returnedDocs.filter( o => o.repeatableGroup.re

我在下面的代码中有一个returnedDocs数组,通过让returnedDocs=result.application.modules.moduleItem缩短;作为API的响应,它的结构非常复杂。有些嵌套对象是数组,有些值放置在结构的几层深处

我使用filter方法只获取获得特定值的元素

          let toFilterSix = returnedDocs.filter(
            o =>
              o.repeatableGroup.repeatableGroupItem.vocabularyReference
                .vocabularyReferenceItem.formattedValue._text === "Abdomen"
          );
          this.filterArray6 = toFilterSix;
通常它工作正常,但在下面的示例中,repeatableGroupItem中的一个是一个包含两个元素的数组

由于过滤对象中存在意外数组,我出现了一个错误:

Results.vue?82a0:202 Uncaught (in promise) TypeError: Cannot read property 'vocabularyReferenceItem' of undefined
    at eval (Results.vue?82a0:202)
    at Array.filter (<anonymous>)
    at eval (Results.vue?82a0:201)
在对其他非数组元素进行筛选时,如何避免错误


这里您可以检查数据模型,它是console.log'ed:

如果您只想忽略数组,您可以简单地测试repeatableGroup是否具有vocabularyReferences属性

let toFilterSix = returnedDocs.filter(
  o =>
  o.repeatableGroup.repeatableGroupItem.vocabularyReference &&
  o.repeatableGroup.repeatableGroupItem.vocabularyReference.vocabularyReferenceItem.formattedValue._text === "Abdomen"
);
如果您还想搜索数组,那么可以在数组为数组时使用If语句来搜索该数组

let toFilterSix = returnedDocs.filter(
  o => {
    if (Array.isArray(o.repeatableGroup.repeatableGroupItem)) {
      return o.repeatableGroup.repeatableGroupItem.some(el => el.vocabularyReference.vocabularyReferenceItem.formattedValue._text === "Abdomen");
    } else {
      return o.repeatableGroup.repeatableGroupItem.vocabularyReference.vocabularyReferenceItem.formattedValue._text === "Abdomen";
    }
  });

如果您试图查找腹部,而不管它是单个对象还是这些对象的数组,则可以使用[].concatrepeatableGroupItem。这允许您安全地假设您正在处理一个数组,而不是单独处理每个案例

从那里你可以使用。一些来确定腹部是否存在于任何物品中

//此数组包含4个对象。。。 //1:repeatableGroupItem作为单个对象,包含 //2:repeatableGroupItem作为对象数组,包含 //3:repeatableGroupItem作为单个对象,无腹部 //4:repeatableGroupItem作为对象数组,没有腹部 const returnedDocs=[{repeatableGroup:{repeatableGroupItem:{vocabularyReferenceItem:{formattedValue:{Utext:腹部}}}}}}}}}},{repeatableGroup:{repeatableGroupItem:[{vocabularyReferenceItem:{vocabularyReferenceItem:{formattedValue:{Utext:腹部}}}}},{vocabularyReferenceItem:{formattedValue:{{U-text:Not-Abstry}}}}}},{repeatableGroup:{repeatableGroupItem:{vocabularyReference:{vocabularyReferenceItem:{formattedValue:{U-text:Not-Abstry}}}}}},{repeatableGroup:{repeatableGroupItem:{vocabularyReferenceItem:{vocabularyReferenceItem:{formattedValue:{,{vocabularyReference:{vocabularyReferenceItem:{formattedValue:{{u text:Not abaster}}}}]}]; const result=returnedDocs.filtero=>{ const item=[].concato.repeatableGroup.repeatableGroupItem; return item.somei=>i.vocabularyReference.vocabularyReferenceItem.formattedValue.\u text===1; }; //记录项目1和2
console.logresult;请提供一个数据模型的示例,如果可能的话,尝试重新表述并简化您的问题-目前来看,这有点难以解释。您可能希望在下一步之前查看以测试目标属性的存在性。非常感谢!这节省了我使用第二个代码段的时间我还有一个关于过滤和API返回的不存在元素的异常的其他瓶颈的起点。谢谢,这是一个很好的研究和使用案例。我的过滤器中有更多未定义的东西:没问题!我写这篇文章是为了更清楚地解释.concat在做什么。