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

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


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


You're effectively about halfway there, you just need to replace the specific index with one that increases. You can just loop over the array (using a for, for...of or while loop): if you're unsure of how to do this, there are a vast number of guides to the basics, so definitely have a Google around: here's the MDN introductory article in iteration, which is pretty good: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Looping_code

The array method "map" would be the more sensible, declarative way to do what you want to do, but if you're stuck at this point then you may find map is slightly more confusing, as it depends on understanding how higher-order functions work.


Use Array.map() to flatten the object. I simplified the object (Works the same) and used ternary operators inside the returned object literal in case any of the properties didn't exist.

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)