当前位置: 首页 > 知识库问答 >
问题:

Javascript返回值在循环中只返回一次

荀增
2023-03-14

我创建了这个函数来计算我要测量的距离:

distanceToResponder: function() {
    var distance = 0;
    var incidentLocation = this.createCoordinate(this.currentIncident["latitude"], this.currentIncident["longitude"]);

    this.responders.forEach(function(responder) {
        var responderLocation = this.createCoordinate(responder.location.latitude, responder.location.longitude);
        distance = this.calculateDistanceBetweenLocations(incidentLocation, responderLocation);
    }.bind(this));
    return distance;
}

由于我不明白的原因,距离的值只返回一次。因此,如果我在forEach函数中抛出2个位置,并且在循环中使用console.log,我会看到所有的计算都是正确的。但如果我返回距离的值,则只返回一个值。

如何正确返回值?

我使用VueJS,其中'Distance toResponder'是一个计算属性来填充表:

   <tr v-for="responder in responders">
        <td>{{ responder.userReference }}</td>
        <td>{{ distanceToResponder(responder) }}</td>
        <td><button type="button" class="btn btn-danger" @click="createDispatch(responder)">Alarm</button></td>
    </tr>

因此函数应该返回每个响应者的距离。那么我需要如何调整我的功能呢?

共有2个答案

干照
2023-03-14

除了外部most函数调用之外,这里根本没有使用返回值。您的内部foreach回调只设置两个函数共享的单个变量(distance)的状态,最终返回该变量。

foreach的每次迭代都会覆盖distance的前一个值,以便只留下最终值。

不清楚“返回多个”是什么意思,因为函数可能只返回一个值,但可以使用map而不是foreach返回距离数组:

distanceToResponder: function() {
    var incidentLocation = this.createCoordinate(this.currentIncident["latitude"], this.currentIncident["longitude"]);

    return this.responders.map(function(responder) {
        var responderLocation = this.createCoordinate(responder.location.latitude, responder.location.longitude);
        return this.calculateDistanceBetweenLocations(incidentLocation, responderLocation);
    }.bind(this));

}
洪育
2023-03-14

您正在迭代您的响应器,并且每次覆盖距离。然后在迭代之后将该距离返回到最后一个响应程序。

    var distances = this.responders.map(function(responder) {
      var responderLocation = this.createCoordinate(responder.location.latitude, responder.location.longitude);
      return this.calculateDistanceBetweenLocations(incidentLocation, responderLocation);
    }.bind(this));
    return distances;

由此创建一个计算属性“distanceResponders”。并且在模板中

 <tr v-for="(index, responder) in responders">
    <td>{{ responder.userReference }}</td>
    <td>{{ distancesToResponder[index] }}</td>
    <td><button type="button" class="btn btn-danger" @click="createDispatch(responder)">Alarm</button></td>
</tr>

或者创建“增强的”响应程序计算属性:

 respondersWithDistance: function() {

  var incidentLocation = this.createCoordinate(this.currentIncident["latitude"], this.currentIncident["longitude"]);

  return this.responders.map(function(responder) {
    var responderLocation = this.createCoordinate(responder.location.latitude, responder.location.longitude);
    responder.distance = this.calculateDistanceBetweenLocations(incidentLocation, responderLocation);
 }.bind(this));
}

并在模板中也使用它:

 <tr v-for="responder in respondersWithDistance">
    <td>{{ responder.userReference }}</td>
    <td>{{ responder.distance }}</td>
    <td><button type="button" class="btn btn-danger" @click="createDispatch(responder)">Alarm</button></td>
</tr>
 类似资料:
  • 在下面的示例中,someObjects是一个集合。如果循环中的条件匹配,我试图返回true,但这似乎无法编译。然而,当我添加“return”时,它工作得很好。我需要解决的问题是什么? 编译错误 类型Iterable中的方法forEach(Consumer)不适用于参数((obj)->{})

  • 问题内容: 今天,有人陪我一起滥用Java 中的关键字。我编写了一个简单的循环来验证数组中是否存在某些内容。假设是一个length数组,这是我的代码: 现在有人告诉我这不是一个很好的编程,因为我在循环内使用了该语句,这将导致垃圾回收发生故障。因此,更好的代码将是: 问题是我无法正确解释为什么第一个for循环不是一个好习惯。有人可以给我一个解释吗? 问题答案: 现在有人告诉我这不是一个很好的编程,因

  • loop 有个用途是尝试一个操作直到成功为止。若操作返回一个值,则可能需要将其传递给代码的其余部分:将该值放在 break 之后,并由 loop 表达式返回。 fn main() { let mut counter = 0; let result = loop { counter += 1; if counter == 10 {

  • 问题内容: 我试图在 JavaScript中 返回两个值。这可能吗? 问题答案: 否,但是您可以返回一个包含您的值的数组: 然后,您可以像这样访问它们: 使用最新的ECMAScript 6语法 *,您还可以更直观地分解返回值: 如果要在每个返回值上贴上“标签”(易于维护),则可以返回一个对象: 并访问它们: 或使用ES6语法:

  • 问题是,如果我输入一个正数(所以一切都正确),我必须输入它两次!同样,当我输入一个负数时,我需要输入两次。当我输入一个字符串时,我会得到一个“错误”,所以我想这还算不错。它是这样显示的: Bitte geben Sie die kleinste阳性Zahl des Intervalls Ein:-2 -3 -7 Bitte Geben Sie eine阳性Zahl ein 2

  • 为了澄清所使用的输入是100,用于投资金额,利率为5%(在该程序中,取5/100,然后/12=0.00417),在这种情况下,投资的持续时间为6个月。因此,在任何利息累积之前的第0个月,没有利息,所以这只是投资的投入金额(100)。然后,在第一个月,它现在运行正常((100*i)*(i interest());或在或情况下((100*1)*(10.00417))=100.417 但当我到了第二个月