jQuery函数返回空值

时间:2016-08-13 11:38:06

标签: javascript jquery

我遇到的问题是函数不返回创建的对象。如果我想要那个变量"测试"我该怎么做?将是创建的同一个对象?

     var test = runSnapToRoad(path);    // pass data to function
     console.log(test);                 // return "undefined"


         function runSnapToRoad(path) {
           snappedPath = [];
            var pathValues = [];
            for (var i = 0; i < path.getLength(); i++) {
              pathValues.push(path.getAt(i).toUrlValue());
            }

            $.get('https://roads.googleapis.com/v1/snapToRoads', {
              interpolate: true,
              key: apiKey,
              path: pathValues.join('|')
            }, function (data) {
              $.each( data.snappedPoints, function( key, value ) {


                var myObj = {
                  "lng" : value.location.longitude,    
                  "lat" : value.location.latitude  
                };

                snappedPath.push( myObj );

              });

              console.log(snappedPath); // until here everything is ok, I got [Object, Object] with objects inside

              return snappedPath; // 
            });

0 个答案:

没有答案