我不能用ajax打破我的foreach

时间:2016-06-28 15:18:27

标签: arrays foreach break

我有这个:

var addresses_tmp = addresses.slice();
    var final_addresses = [];
    addresses.forEach(function (current_address, i) {
        var near_addresses = [];
        near_addresses.push(current_address);
        addresses_tmp.forEach(function (next_address, j) {
            if (current_address.address.info.code != next_address.address.info.code) {
                var data = {
                    key             : $scope.MicrosoftKey,
                    optmz           : "distance",
                    routeAttributes : "routePath"
                }
                data["wp.0"] = current_address.address.location.lat + "," + current_address.address.location.lng;
                data["wp.1"] = next_address.address.location.lat + "," + next_address.address.location.lng;
                ajax.sendApiRequest(data, "GET", "http://dev.virtualearth.net/REST/V1/Routes/Driving", is_url=true).then(
                    function(response) {
                        var distance = response.data.resourceSets[0].resources[0].travelDistance;
                        if (distance < 0.020) {
                            near_addresses.push(next_address);
                            addresses_tmp.splice(j, 1);
                        }
                        if (j == addresses.length - 1) {
                            final_addresses.push(near_addresses);
                            if (near_addresses.length == 1) {
                                addresses_tmp.splice(j, 1);
                            }
                            addresses_tmp.splice(i, 1);

                        }

                        // if (count == addresses.length * addresses.length) {
                        //     console.log("ya he acabado todasssss")

                        // }
                    },
                    function(error) {
                        console.log("error", error);
                    }
                )
            }
        })
    })

我想在第一个foreach和第二个foreach完成时打破所有功能但是我不能放置条件这样做。

由于我在第二个foreach中有ajax,我的变量很疯狂所以我不能设置if条件来打破它。

我需要这样做,因为我比较两个数组并获得两点之间的距离(一个在第一个数组中,第二个在另一个数组中)

0 个答案:

没有答案