2

我正在使用 TomTom places api 作为项目的一部分。我是 Javascript 新手,我发现很难从 API 中提取我需要的信息。我正在尝试从结果数组中获取所有姓名、电话号码、地址和网址。这就是我正在使用的 -

"summary": {
  "query": "restaurant",
  "queryType": "NON_NEAR",
  "queryTime": 126,
  "numResults": 10,
  "offset": 0,
  "totalResults": 102131,
  "fuzzyLevel": 1,
  "geoBias": {
    "lat": 50.266,
    "lon": 5.0527
  }
},
"results": [{
  "type": "POI",
  "id": "GB/POI/p0/1035734",
  "score": 2.1523399353027344,
  "dist": 277294.490777698,
  "info": "search:ta:826009007710588-GB",
  "poi": {
    "name": "Bay Restaurant",
    "phone": "+(44)-(1304)-852229",
    "categorySet": [{
      "id": 7315008
    }],
    "url": "thewhitecliffs.com",
    "categories": [
      "british",
      "restaurant"
    ],
    "classifications": [{
      "code": "RESTAURANT",
      "names": [{
          "nameLocale": "en-US",
          "name": "restaurant"
        },
        {
          "nameLocale": "en-US",
          "name": "british"
        }
      ]
    }]
  },
  "address": {},
  "position": {
    "lat": 51.15375,
    "lon": 1.37204
  },
  "viewport": {
    "topLeftPoint": {
      "lat": 51.15465,
      "lon": 1.37061
    },
    "btmRightPoint": {
      "lat": 51.15285,
      "lon": 1.37347
    }
  },
  "entryPoints": [{
    "type": "main",
    "position": {
      "lat": 51.15375,
      "lon": 1.37204
    }
  }]
}

到目前为止,我已经设法通过以下方式获得结果第一名的名称 -

function callbackFn(result) {
  console.log(result[0].poi.name)
}

我已经设法对电话号码做同样的事情 -

function callbackFn(result) {
  console.log(result[0].poi.phone)
}

但是,我不知道如何获取所有给定结果的数据。给出的结果数量取决于搜索条件,所以我希望能够获得所有结果的名称、电话号码、地址和网址,无论是 8 还是 100。

4

3 回答 3

3

您可以使用Array.prototype.map()带有对象解构的方法来选择所需的特定属性:

function callbackFn(results) {
  const data = results.map(result => {
    const { poi, address } = result;
    const { name, phone, url } = poi;
    return { name, phone, address, url };
  });

  console.log(data);
}

const results = [{
  "type": "POI",
  "id": "GB/POI/p0/1035734",
  "score": 2.1523399353027344,
  "dist": 277294.490777698,
  "info": "search:ta:826009007710588-GB",
  "poi": {
    "name": "Bay Restaurant",
    "phone": "+(44)-(1304)-852229",
    "categorySet": [{
      "id": 7315008
    }],
    "url": "thewhitecliffs.com",
    "categories": [
      "british",
      "restaurant"
    ],
    "classifications": [{
      "code": "RESTAURANT",
      "names": [{
          "nameLocale": "en-US",
          "name": "restaurant"
        },
        {
          "nameLocale": "en-US",
          "name": "british"
        }
      ]
    }]
  },
  "address": {},
  "position": {
    "lat": 51.15375,
    "lon": 1.37204
  },
  "viewport": {
    "topLeftPoint": {
      "lat": 51.15465,
      "lon": 1.37061
    },
    "btmRightPoint": {
      "lat": 51.15285,
      "lon": 1.37347
    }
  },
  "entryPoints": [{
    "type": "main",
    "position": {
      "lat": 51.15375,
      "lon": 1.37204
    }
  }]
}]

callbackFn(results);

于 2020-01-03T19:12:38.773 回答
2

您实际上已经完成了一半,您只需要将特定索引替换为增加的索引。您可以循环遍历数组(使用 for、for...of 或 while 循环):如果您不确定如何执行此操作,则有大量的基础指南,所以一定要有 Google :这是迭代中的 MDN 介绍性文章,非常好:https ://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Looping_code

数组方法“map”将是做你想做的事情的更明智、声明性的方法,但如果你被困在这一点上,那么你可能会发现map有点混乱,因为它取决于理解高阶函数的方式工作。

于 2020-01-03T19:02:22.670 回答
1

使用 Array.map() 展平对象。我简化了对象(工作相同)并在返回的对象文字中使用三元运算符,以防任何属性不存在。

let obj = {

  "summary": {
    "query": "restaurant"
  },
  "results": [{
      "poi": {
        "name": "Bay Restaurant",
        "phone": "+(44)-(1304)-852229",
        "url": "thewhitecliffs.com",
      },
      "address": {
        "stAddr": "1234 main st",
        "city": "Your City",
        "state": "Your State"
      },
    },
    {
      "poi": {
        "name": "Other Bay Restaurant",
        "phone": "Other +(44)-(1304)-852229",
        "url": "Other thewhitecliffs.com",
      },
      "address": {
        "stAddr": "Other 1234 main st",
        "city": "Other Your City",
        "state": "Other Your State"
      },
    }
  ]
}

let result = obj.results.map(el => {
    return {
        name: el.poi && el.poi.name ? el.poi.name : "",
        phone: el.poi && el.poi.phone ? el.poi.phone : "",
        url: el.poi && el.poi.url ? el.poi.url : "",
        stAddr: el.address && el.address.stAddr ? el.address.stAddr : "",
        city: el.address && el.address.city ? el.address.city : "",
        state: el.address && el.address.state ?  el.address.state : ""
    }
})

console.log(result)

于 2020-01-03T19:25:40.153 回答