代码之家  ›  专栏  ›  技术社区  ›  Tom

使用Angular7中的typescript查找至少一个与数组中的条件匹配的元素

  •  -1
  • Tom  · 技术社区  · 6 年前

    我基本上需要寻找 true

    如果你在下面注意到 this.DocumentSelected[i] = true; 在地图里。因此,我试图初始化数组并检查是否有一个值包含 真的 然后退出该方法。

    DocumentSelected: any = [];
    
    this.files.map(doc => {
      if (doc.selectedDocumentItem.Id === null) {
        this.DocumentSelected[i] = true;
        return;
      }
    
      const datestr = (new Date(doc.selectedDate)).toUTCString();
    
      formData.append('documentTypeId' + i++, doc.selectedDocumentItem.Id.toString());
      formData.append('documentDate' + j++, datestr);
    
      const fileEntry = doc.fileDropEntry.fileEntry as FileSystemFileEntry;
    
      fileEntry.file((file: File) => {
        formData.append('file' + k++, file, doc.name);
      });
    });
    
    if (this.DocumentSelected) {
      this.notify.error('Please select the Document Type');
      return;
    }
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   Andrew Hill    6 年前

    Array.prototype.map() 将迭代一个数组并返回另一个数组。

    你不用 map()

    为了纯粹的迭代目的,您应该使用 Array.prototype.forEach() 或者传统的 for 如果需要访问 i

    for (let i = 0; i < this.files.length; i++) {
      const doc = this.files[i];
    
      if (doc.selectedDocumentItem.Id === null) {
        this.DocumentSelected[i] = true;
        break; // <-- terminate for loop
      }
    }