提取 Yelp 的评分

Extract Yelp's rating

我有这个 locations.json 文件,我在其中存储标题、位置(纬度、经度)和 phone 数字。我现在面临的问题对其他人来说可能看起来微不足道,但作为初学者,我无法让它按照我喜欢的方式工作。我只想从 Yelp 的 api v3 中提取评分并将其添加到 locations.rating 数组。我在下面的代码附加了来自 Yelp 的整个响应 object return,但是当我尝试 console.log (response.businesses[0].rating) 时,它是只能打印出评分。我怎样才能让它 return 只有评级?提前致谢。

var yelpPhoneSearch = "https://api.yelp.com/v3/businesses/search/phone?phone=";
var cors_anywhere_url = 'https://cors-anywhere.herokuapp.com/';  // Yelp v3 api doesn't support CORS, need to use this 3rd party proxy service
var locations = [];
$.getJSON('/locations.json', function(data){
  for (var i = 0; i < data.length; i++) {
    var schools = {};
    schools.title = data[i].title;
    schools.location = data[i].location;
    schools.phone = data[i].phone;
    schools.rating = $.ajax({
      "async": true,
      "crossDomain": true,
      "url": cors_anywhere_url + yelpPhoneSearch + data[i].phone,
      "method": "GET",
      "headers": {
        "authorization": "Bearer " + yelpToken.access_token,
        "cache-control": "public, max-age=31536000",
      }
    }).done(function(response){
      // console.log(response);
      var rating = response.businesses[0].rating;
      return rating;
    });
    // Push all infos to locations array
    locations.push(schools);
  }
});

使用 jQuery promises(除了保证在旧浏览器中可用外,这几乎与常规 Promises 类似)您可以像这样重写代码

var yelpPhoneSearch = "https://api.yelp.com/v3/businesses/search/phone?phone=";
var cors_anywhere_url = 'https://cors-anywhere.herokuapp.com/';  // Yelp v3 api doesn't support CORS, need to use this 3rd party proxy service

$.getJSON('/locations.json')
.then(function(data){
    return $.when.apply($, data.map(function(school) {
        return $.ajax({
            "async": true,
            "crossDomain": true,
            "url": cors_anywhere_url + yelpPhoneSearch + school.phone,
            "method": "GET",
            "headers": {
                "authorization": "Bearer " + yelpToken.access_token,
                "cache-control": "public, max-age=31536000",
            }
        }).then(function(response) {
            return {
                title: school.title,
                location: school.location,
                phone: school.phone,
                rating: response.businesses[0].rating
            };
        });
    }));
}).then(function() {
    var locations = [].slice.call(arguments);
    /* here locations is an array of 
    {
        title,
        location,
        phone,
        rating
    }
    */
});

注意:由于 $.ajax 的异步性质,您无法访问最后一个 .then 之外的结果数组,因为异步代码是异步的

为了完整性 - 带有原生 Promises 的 ES2015+,代码将是

$.getJSON('/locations.json').then(data => 
    Promise.all(data.map(school => 
        $.ajax({
            "async": true,
            "crossDomain": true,
            "url": cors_anywhere_url + yelpPhoneSearch + school.phone,
            "method": "GET",
            "headers": {
                "authorization": "Bearer " + yelpToken.access_token,
                "cache-control": "public, max-age=31536000",
            }
        }).then(response => ({
            title: school.title,
            location: school.location,
            phone: school.phone,
            rating: response.businesses[0].rating
        }))
    ))
).then(locations => {
    /* here locations is an array of 
    {
        title,
        location,
        phone,
        rating
    }
    */
});

如果您需要在代码的其他部分访问 locations,并且假设您问题中的代码不是从函数中获取的(即变量都是全局范围的)

var yelpPhoneSearch = "https://api.yelp.com/v3/businesses/search/phone?phone=";
var cors_anywhere_url = 'https://cors-anywhere.herokuapp.com/';  // Yelp v3 api doesn't support CORS, need to use this 3rd party proxy service

var locations = $.getJSON('/locations.json')
.then(function(data){
    return $.when.apply($, data.map(function(school) {
        return $.ajax({
            "async": true,
            "crossDomain": true,
            "url": cors_anywhere_url + yelpPhoneSearch + school.phone,
            "method": "GET",
            "headers": {
                "authorization": "Bearer " + yelpToken.access_token,
                "cache-control": "public, max-age=31536000",
            }
        }).then(function(response) {
            return {
                title: school.title,
                location: school.location,
                phone: school.phone,
                rating: response.businesses[0].rating
            };
        });
    }));
}).then(function() {
    return [].slice.call(arguments);
});

现在,在需要使用位置的代码中,您需要使用典型的 Promise 方法来访问结果

locations.then(function(value) {
    // in here,  value is an array of locations
});

没有看到你在实际代码中是如何使用的locations,这可能不像上面那么容易,因为,一旦你处理了异步代码,你需要适当地处理它