javascriptarraysjsontomtom

How to get all objects of same type inside an array in Javascript?


I am working with the TomTom places api as part of a project. I am new to Javascript and I'm finding it difficult to extract the information I need from the API. I am trying to get all names, phone numbers, addresses and urls from the results array. This is what I'm working with -

"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
    }
  }]
}

So far, I've managed to get the name for result number one by doing -

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

And I've managed to do the same for the phone number by doing -

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

However, I do not know how to grab that data for all of the results given. The number of results given varies depending on the search criteria, so I'd like to preferably be able to get the names, phone numbers, addresses and urls of all the results whether there are 8 or 100.


Solution

  • You can use the Array.prototype.map() method with object destructuring to select the specific properties you want:

    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);