Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅在forEach循环的最后一次迭代中触发函数_Javascript_Loops - Fatal编程技术网

Javascript 仅在forEach循环的最后一次迭代中触发函数

Javascript 仅在forEach循环的最后一次迭代中触发函数,javascript,loops,Javascript,Loops,下面的代码检查字段是否为file类型,以及其中是否有实际文件。如果是这样,上传照片并更新建筑物。如果不是这样,请使用旧照片更新建筑: fields.forEach(field => { building[field.name] = field.value || this.building[field.name] if (field.type === 'file' && !util.isEmpty(field.photo.file)) { ap

下面的代码检查字段是否为file类型,以及其中是否有实际文件。如果是这样,上传照片并更新建筑物。如果不是这样,请使用旧照片更新建筑:

  fields.forEach(field => {
    building[field.name] = field.value || this.building[field.name]
    if (field.type === 'file' && !util.isEmpty(field.photo.file)) {
      api.uploadPhoto(field.photo.file).then(resp => {
        building[field.name] = resp
        this.updateBuilding(building)
      })
    } else {
      building.logo = this.building.logo // to prevent updating logo
      building.floorplan = this.building.floorplan // to prevent updating logo
      this.updateBuilding(building)
    }
  })
它工作得很好,但是由于
this.updateBuilding(building)
是一个循环,所以它被称为multiples


如何使其仅在上一次迭代中调用?

在每次迭代中使用带增量的计数变量,如果field.length等于count变量,则使用if条件调用函数

var count = 0;
var fields_length = fields.length;
fields.forEach(field => {
    building[field.name] = field.value || this.building[field.name]
    if (field.type === 'file' && !util.isEmpty(field.photo.file)) {
      api.uploadPhoto(field.photo.file).then(resp => {
        building[field.name] = resp

      })
    } else {
      building.logo = this.building.logo // to prevent updating logo
      building.floorplan = this.building.floorplan // to prevent updating logo

    }
    if(count == fields_length){
       this.updateBuilding(building)
    }

count++
  })

在每次迭代中使用带有增量的计数变量,如果field.length等于count变量,则使用if条件调用函数

var count = 0;
var fields_length = fields.length;
fields.forEach(field => {
    building[field.name] = field.value || this.building[field.name]
    if (field.type === 'file' && !util.isEmpty(field.photo.file)) {
      api.uploadPhoto(field.photo.file).then(resp => {
        building[field.name] = resp

      })
    } else {
      building.logo = this.building.logo // to prevent updating logo
      building.floorplan = this.building.floorplan // to prevent updating logo

    }
    if(count == fields_length){
       this.updateBuilding(building)
    }

count++
  })

您可以检查字段。字段数组的长度,它将是字段数组的最后一个元素

if(fields[fields.length - 1]){
 // Loop control will enter in this block in last iteration
 // Enter your update code inside this
}

希望这就是您要查找的内容。

您可以检查字段。字段数组的长度,它将是字段数组的最后一个元素

if(fields[fields.length - 1]){
 // Loop control will enter in this block in last iteration
 // Enter your update code inside this
}
希望这就是您想要的。

试试:

var len = fields.length;
var counter = 0;

fields.forEach(field => {
    building[field.name] = field.value || this.building[field.name]
    if (field.type === 'file' && !util.isEmpty(field.photo.file)) {
        api.uploadPhoto(field.photo.file).then(resp => {
            building[field.name] = resp

            /* checking if it's the last loop */
            if(counter == len - 1)
                this.updateBuilding(building)
        })
    } else {
        building.logo = this.building.logo // to prevent updating logo
        building.floorplan = this.building.floorplan // to prevent updating logo
  this.updateBuilding(building)
    }

    counter = counter + 1;
})
尝试:


如何创建一些变量来在循环中保存适当的值; 循环完成后,可以使用这些变量在构建时运行更新

更新


哦,你需要回调才能更新您在循环中得到了一些异步操作???

您如何创建一些变量以在循环中保存适当的值; 循环完成后,可以使用这些变量在构建时运行更新

更新

哦,你真的需要回拨更新循环中有一些异步操作???

建议对
forEach
的回调有三个参数:

  • 元素值
  • 元素索引
  • 正在遍历的数组
  • 您可以使用它来检查当前元素的索引是否等于数组的最后一个索引:

    fields.forEach((field, index, array) => {
    
      // YOUR CODE
    
      if (index === array.length - 1) {
        // DO SOMETHING
      }
    });
    
    建议对
    forEach
    的回调有三个参数:

  • 元素值
  • 元素索引
  • 正在遍历的数组
  • 您可以使用它来检查当前元素的索引是否等于数组的最后一个索引:

    fields.forEach((field, index, array) => {
    
      // YOUR CODE
    
      if (index === array.length - 1) {
        // DO SOMETHING
      }
    });
    

    fields.length-1
    因为JavaScript是零索引的?等等,这样我就可以得到元素了。我得把它和别的东西比较一下?(现在循环从未进入if语句。)
    fields.length-1
    因为JavaScript是零索引的?等等,这样我就可以得到元素了。我得把它和别的东西比较一下?(现在循环从未进入if语句。)您使用的是
    if(count==fields.length)
    。这将遍历整个集合以计算每次的长度。最好在循环外计算一次长度,并将
    count
    与此值进行比较。您使用的是
    if(count==fields.length)
    。这将遍历整个集合以计算每次的长度。最好在循环外计算一次长度,然后将
    count
    与该值进行比较。我不敢相信我已经使用JavaScript多年了,我不知道
    forEach
    有索引和数组值。谢谢我不敢相信我已经做了多年JavaScript,我不知道
    forEach
    有索引和数组值。谢谢