Related
I am getting data from 2 APIs, one returns the data from 18 different airports, and one returns the flighst from one airport to another (this API needs to be fed with the id of the departure airport and the arrival airport)
The API that returns the flights is like this one here:
{
"data": [
{
"id": 64,
"airlineId": 8,
"departureAirportId": 5,
"arrivalAirportId": 2,
"price": 205.75
},
{
"id": 42,
"airlineId": 7,
"departureAirportId": 2,
"arrivalAirportId": 17,
"price": 1324.42
}
]
}
While The API that returns the airports is this one:
{
"data": [
{
"id": 1,
"codeIata": "PSA",
"latitude": 43.6931,
"longitude": 10.3789
},
{
"id": 2,
"codeIata": "BLQ",
"latitude": 44.5357,
"longitude": 11.2736
},
{
"id": 3,
"codeIata": "BGY",
"latitude": 45.6702,
"longitude": 9.6715
},
{
"id": 4,
"codeIata": "MXP",
"latitude": 45.6289,
"longitude": 8.7183
},
{
"id": 5,
"codeIata": "TRN",
"latitude": 45.1934,
"longitude": 7.6158
},
{
"id": 6,
"codeIata": "VCE",
"latitude": 45.5046,
"longitude": 12.3444
},
{
"id": 7,
"codeIata": "FCO",
"latitude": 41.8025,
"longitude": 12.213
},
{
"id": 8,
"codeIata": "PSR",
"latitude": 42.4261,
"longitude": 14.1793
},
{
"id": 9,
"codeIata": "NAP",
"latitude": 40.8819,
"longitude": 14.2731
},
{
"id": 10,
"codeIata": "BRI",
"latitude": 41.1375,
"longitude": 16.763
},
{
"id": 11,
"codeIata": "BDS",
"latitude": 40.6551,
"longitude": 17.936
},
{
"id": 12,
"codeIata": "SUF",
"latitude": 38.9057,
"longitude": 16.2402
},
{
"id": 13,
"codeIata": "CTA",
"latitude": 37.4667,
"longitude": 15.0639
},
{
"id": 14,
"codeIata": "PMO",
"latitude": 38.1813,
"longitude": 13.0994
},
{
"id": 15,
"codeIata": "NRT",
"latitude": 35.7653,
"longitude": 140.386
},
{
"id": 16,
"codeIata": "KLX",
"latitude": 37.0683,
"longitude": 22.0256
},
{
"id": 17,
"codeIata": "ALA",
"latitude": 43.3553,
"longitude": 77.0447
},
{
"id": 18,
"codeIata": "PEK",
"latitude": 40.0725,
"longitude": 116.5975
}
]
}
I need to take the "departureAirportId" and the "arrivalAirportId" from the flights API, check them against the airport API and fetch the name of the airport according the provided id.
I tried in many ways but each time I ended up digging a huge rabbit hole and decided just to restart all over for I do not know how many times now.
Any suggestion how I could do this?
This should solve your problem:
let flights = {
"data": [{
"id": 64,
"airlineId": 8,
"departureAirportId": 5,
"arrivalAirportId": 2,
"price": 205.75
},
{
"id": 42,
"airlineId": 7,
"departureAirportId": 2,
"arrivalAirportId": 17,
"price": 1324.42
}
]
};
let airports = {
"data": [{
"id": 1,
"codeIata": "PSA",
"latitude": 43.6931,
"longitude": 10.3789
},
{
"id": 2,
"codeIata": "BLQ",
"latitude": 44.5357,
"longitude": 11.2736
},
{
"id": 3,
"codeIata": "BGY",
"latitude": 45.6702,
"longitude": 9.6715
},
{
"id": 4,
"codeIata": "MXP",
"latitude": 45.6289,
"longitude": 8.7183
},
{
"id": 5,
"codeIata": "TRN",
"latitude": 45.1934,
"longitude": 7.6158
},
{
"id": 6,
"codeIata": "VCE",
"latitude": 45.5046,
"longitude": 12.3444
},
{
"id": 7,
"codeIata": "FCO",
"latitude": 41.8025,
"longitude": 12.213
},
{
"id": 8,
"codeIata": "PSR",
"latitude": 42.4261,
"longitude": 14.1793
},
{
"id": 9,
"codeIata": "NAP",
"latitude": 40.8819,
"longitude": 14.2731
},
{
"id": 10,
"codeIata": "BRI",
"latitude": 41.1375,
"longitude": 16.763
},
{
"id": 11,
"codeIata": "BDS",
"latitude": 40.6551,
"longitude": 17.936
},
{
"id": 12,
"codeIata": "SUF",
"latitude": 38.9057,
"longitude": 16.2402
},
{
"id": 13,
"codeIata": "CTA",
"latitude": 37.4667,
"longitude": 15.0639
},
{
"id": 14,
"codeIata": "PMO",
"latitude": 38.1813,
"longitude": 13.0994
},
{
"id": 15,
"codeIata": "NRT",
"latitude": 35.7653,
"longitude": 140.386
},
{
"id": 16,
"codeIata": "KLX",
"latitude": 37.0683,
"longitude": 22.0256
},
{
"id": 17,
"codeIata": "ALA",
"latitude": 43.3553,
"longitude": 77.0447
},
{
"id": 18,
"codeIata": "PEK",
"latitude": 40.0725,
"longitude": 116.5975
}
]
};
const findAirportByID = (airportID) => {
return airports.data.filter(item => item.id == airportID);
}
flights.data.forEach(flight => {
let departureAirport = findAirportByID(flight.departureAirportId);
if(departureAirport[0])
flight.deparureAirportCode = departureAirport[0].codeIata;
let arrivalAirport = findAirportByID(flight.arrivalAirportId);
if(arrivalAirport[0])
flight.arrivalAirportCode = arrivalAirport[0].codeIata;
});
console.log(flights);
I would like to collect all info which can be fetched from user without ask any permissions.
I don't want to use any of non-free ip info services and Geolocation
What I have now:
timeZoneOffset = new Date().getTimezoneOffset();
userLang = navigator.language || navigator.userLanguage;
But I want more info about users location.
Solution 1: The HTML Geolocation API is used to get the geographical position of a user.
But it asked user for permission.
Solution 2: Free IP Location API
Check this: https://ipdata.co/
$.getJSON('https://geolocation-db.com/json/')
.done (function(location) {
$('#country').html(location.country_name);
$('#state').html(location.state);
$('#city').html(location.city);
$('#latitude').html(location.latitude);
$('#longitude').html(location.longitude);
$('#ip').html(location.IPv4);
});
Solution 3: PAID IP based API
There is no other solution for this.
The other answers mention paid services (ipdata) or free services without the data you need - timezone, etc - (geolocation-db.com)
I believe Abstract has a free plan for their IP geolocation API that includes timezone data among others: https://www.abstractapi.com/ip-geolocation-api
$.getJSON("https://ipgeolocation.abstractapi.com/v1/?api_key=****************", function(data) {
console.log(data.ip_address);
console.log(data.country);
console.log(data.timezone);
})
You can use BigDataCloud's IP Geolocation API to get maximum data from the IP address of your website visitor.
It provides 10K queries per month for free.
https://www.bigdatacloud.com/ip-geolocation-apis/ip-address-geolocation-api
Output data:
{
"ip": "73.231.131.73",
"localityLanguageRequested": "en",
"isReachableGlobally": true,
"country": {
"isoAlpha2": "US",
"isoAlpha3": "USA",
"m49Code": 840,
"name": "United States of America",
"isoName": "United States of America (the)",
"isoNameFull": "the United States of America",
"isoAdminLanguages": [
{
"isoAlpha3": "eng",
"isoAlpha2": "en",
"isoName": "English",
"nativeName": "English"
}
],
"unRegion": "Americas/Northern America",
"currency": {
"numericCode": 840,
"code": "USD",
"name": "US Dollar",
"minorUnits": 2
},
"wbRegion": {
"id": "NAC",
"iso2Code": "XU",
"value": "North America"
},
"wbIncomeLevel": {
"id": "HIC",
"iso2Code": "XD",
"value": "High income"
},
"callingCode": "1",
"countryFlagEmoji": "πΊπΈ"
},
"location": {
"continent": "North America",
"continentCode": "NA",
"isoPrincipalSubdivision": "California",
"isoPrincipalSubdivisionCode": "US-CA",
"city": "Milpitas",
"localityName": "Milpitas",
"postcode": "95035",
"latitude": 37.45,
"longitude": -121.92,
"plusCode": "849WF32J+22",
"timeZone": {
"ianaTimeId": "America/Los_Angeles",
"displayName": "(UTC-08:00) Pacific Standard Time",
"effectiveTimeZoneFull": "Pacific Standard Time",
"effectiveTimeZoneShort": "PST",
"utcOffsetSeconds": -28800,
"utcOffset": "-08",
"isDaylightSavingTime": false,
"localTime": "2021-02-28T17:11:59.1873233"
},
"localityInfo": {
"administrative": [
{
"order": 2,
"adminLevel": 2,
"name": "United States of America",
"description": "country in North America",
"isoName": "United States of America (the)",
"isoCode": "US",
"wikidataId": "Q30",
"geonameId": 6252001
},
{
"order": 4,
"adminLevel": 4,
"name": "California",
"description": "state of the United States of America",
"isoName": "California",
"isoCode": "US-CA",
"wikidataId": "Q99",
"geonameId": 5332921
},
{
"order": 6,
"adminLevel": 6,
"name": "Santa Clara County",
"description": "county in California, United States",
"wikidataId": "Q110739",
"geonameId": 5393021
},
{
"order": 8,
"adminLevel": 8,
"name": "Milpitas",
"description": "city in Santa Clara County, California",
"wikidataId": "Q927510",
"geonameId": 5373327
}
],
"informative": [
{
"order": 1,
"name": "North America",
"description": "continent on the Earth's northwestern quadrant",
"isoCode": "NA",
"wikidataId": "Q49",
"geonameId": 6255149
},
{
"order": 3,
"name": "contiguous United States",
"description": "48 states of the United States apart from Alaska and Hawaii",
"wikidataId": "Q578170"
},
{
"order": 5,
"name": "Pacific Coast Ranges",
"description": "A series of mountain ranges along the Pacific coast of North America",
"wikidataId": "Q660304"
},
{
"order": 7,
"name": "95035",
"description": "postal code"
}
]
}
},
"lastUpdated": "2021-02-28T23:39:21.8284029Z",
"network": {
"registry": "ARIN",
"registryStatus": "assigned",
"registeredCountry": "US",
"registeredCountryName": "United States of America",
"organisation": "Comcast Cable Communications, LLC",
"isReachableGlobally": true,
"isBogon": false,
"bgpPrefix": "73.231.0.0/16",
"bgpPrefixNetworkAddress": "73.231.0.0",
"bgpPrefixLastAddress": "73.231.255.255",
"totalAddresses": 65536,
"carriers": [
{
"asn": "AS33651",
"asnNumeric": 33651,
"organisation": "Comcast Cable Communications LLC",
"name": "CMCS",
"registry": "ARIN",
"registeredCountry": "US",
"registeredCountryName": "United States of America",
"registrationDate": "2005-02-17",
"registrationLastChange": "2021-01-26",
"totalIpv4Addresses": 3147776,
"totalIpv4Prefixes": 217,
"totalIpv4BogonPrefixes": 0,
"rank": 152,
"rankText": "#152 out of 70,908"
}
],
"viaCarriers": [
{
"asn": "AS7922",
"asnNumeric": 7922,
"organisation": "Comcast Cable Communications LLC",
"registeredCountry": "US",
"registeredCountryName": "United States of America",
"totalIpv4Addresses": 24911902,
"rank": 15
}
]
},
"confidence": "high",
"confidenceArea": [
{
"latitude": 37.41,
"longitude": -122.244804
},
{
"latitude": 37.42,
"longitude": -122.244804
},
{
"latitude": 37.483982,
"longitude": -122.23432
},
{
"latitude": 37.513985,
"longitude": -122.22432
},
{
"latitude": 37.557896,
"longitude": -122.20398
},
{
"latitude": 37.598026,
"longitude": -122.17369
},
{
"latitude": 37.632973,
"longitude": -122.134514
},
{
"latitude": 37.66151,
"longitude": -122.08782
},
{
"latitude": 37.68264,
"longitude": -122.03526
},
{
"latitude": 37.69562,
"longitude": -121.97865
},
{
"latitude": 37.7,
"longitude": -121.92
},
{
"latitude": 37.7,
"longitude": -121.89
},
{
"latitude": 37.6962,
"longitude": -121.83531
},
{
"latitude": 37.684914,
"longitude": -121.782295
},
{
"latitude": 37.666485,
"longitude": -121.732544
},
{
"latitude": 37.64147,
"longitude": -121.687584
},
{
"latitude": 37.62147,
"longitude": -121.657585
},
{
"latitude": 37.582153,
"longitude": -121.610214
},
{
"latitude": 37.535625,
"longitude": -121.57468
},
{
"latitude": 37.484097,
"longitude": -121.55265
},
{
"latitude": 37.43,
"longitude": -121.5452
},
{
"latitude": 37.42,
"longitude": -121.5452
},
{
"latitude": 37.364517,
"longitude": -121.55305
},
{
"latitude": 37.311806,
"longitude": -121.5762
},
{
"latitude": 37.26449,
"longitude": -121.61351
},
{
"latitude": 37.25449,
"longitude": -121.62351
},
{
"latitude": 37.22382,
"longitude": -121.65991
},
{
"latitude": 37.19844,
"longitude": -121.70228
},
{
"latitude": 37.188442,
"longitude": -121.722275
},
{
"latitude": 37.16475,
"longitude": -121.78348
},
{
"latitude": 37.15206,
"longitude": -121.84966
},
{
"latitude": 37.150974,
"longitude": -121.91773
},
{
"latitude": 37.161533,
"longitude": -121.98451
},
{
"latitude": 37.17153,
"longitude": -122.02451
},
{
"latitude": 37.17153,
"longitude": -122.02451
},
{
"latitude": 37.19254,
"longitude": -122.085304
},
{
"latitude": 37.223186,
"longitude": -122.1392
},
{
"latitude": 37.262123,
"longitude": -122.18382
},
{
"latitude": 37.307613,
"longitude": -122.21719
},
{
"latitude": 37.357647,
"longitude": -122.23782
},
{
"latitude": 37.41,
"longitude": -122.244804
}
],
"securityThreat": "unknown",
"hazardReport": {
"isKnownAsTorServer": false,
"isKnownAsVpn": false,
"isKnownAsProxy": false,
"isSpamhausDrop": false,
"isSpamhausEdrop": false,
"isSpamhausAsnDrop": false,
"isBlacklistedUceprotect": false,
"isBlacklistedBlocklistDe": false,
"isKnownAsMailServer": false,
"isKnownAsPublicRouter": false,
"isBogon": false,
"isUnreachable": false,
"hostingLikelihood": 0,
"isHostingAsn": false,
"isCellular": false
}
}
I have retrieved a JSON object from a public data API which look similar to following.
[
{
"category": "Burglary",
"location_type": "Force",
"location": {
"latitude": "51.497877",
"street": {
"id": 953834,
"name": "On or near Major Road"
},
"longitude": "-0.064175"
},
"context": "",
"outcome_status": null,
"persistent_id": "",
"id": 53832838,
"location_subtype": "",
"month": "2016-12"
},
{
"category": "anti-social-behaviour",
"location_type": "Force",
"location": {
"latitude": "51.497877",
"street": {
"id": 953834,
"name": "On or near Major Road"
},
"longitude": "-0.064175"
},
"context": "",
"outcome_status": null,
"persistent_id": "",
"id": 53832841,
"location_subtype": "",
"month": "2016-12"
},
{
"category": "anti-social-behaviour",
"location_type": "Force",
"location": {
"latitude": "51.497877",
"street": {
"id": 953834,
"name": "On or near Major Road"
},
"longitude": "-0.064175"
},
"context": "",
"outcome_status": null,
"persistent_id": "",
"id": 53832849,
"location_subtype": "",
"month": "2016-12"
},
{
"category": "anti-social-behaviour",
"location_type": "Force",
"location": {
"latitude": "51.500440",
"street": {
"id": 953881,
"name": "On or near Chambers Street"
},
"longitude": "-0.066891"
},
"context": "",
"outcome_status": null,
"persistent_id": "",
"id": 53832881,
"location_subtype": "",
"month": "2016-12"
} ]
I am trying to count how many crimes happened in each category. Is it correct to access category value as 'obj[0].category' ? What would be the best way to count them?
Assuming you've parsed the JSON to get an array of objects, you can use the array .reduce() method to count the categories. .reduce() calls the function that you pass it once for each item in your array, passing as arguments an "accumulator" acc, which in this case will be an object {}, and the current array value crime.
var data = // your data here
var categories = data.reduce(function(acc, crime) {
if (!acc[crime.category]) // if current category not in acc object
acc[crime.category] = 1; // add it to acc with value 1
else // otherwise (it exists), so
acc[crime.category]++; // increment it
return acc;
}, {});
The result will be an object like this:
{
"Burglary": 1,
"anti-social-behaviour": 3
}
...so if you want to know how many burglaries occurred you could say categories["Burglary"].
(Expand and run the following snippet to see it work...)
var data = [
{
"category": "Burglary",
"location_type": "Force",
"location": {
"latitude": "51.497877",
"street": {
"id": 953834,
"name": "On or near Major Road"
},
"longitude": "-0.064175"
},
"context": "",
"outcome_status": null,
"persistent_id": "",
"id": 53832838,
"location_subtype": "",
"month": "2016-12"
},
{
"category": "anti-social-behaviour",
"location_type": "Force",
"location": {
"latitude": "51.497877",
"street": {
"id": 953834,
"name": "On or near Major Road"
},
"longitude": "-0.064175"
},
"context": "",
"outcome_status": null,
"persistent_id": "",
"id": 53832841,
"location_subtype": "",
"month": "2016-12"
},
{
"category": "anti-social-behaviour",
"location_type": "Force",
"location": {
"latitude": "51.497877",
"street": {
"id": 953834,
"name": "On or near Major Road"
},
"longitude": "-0.064175"
},
"context": "",
"outcome_status": null,
"persistent_id": "",
"id": 53832849,
"location_subtype": "",
"month": "2016-12"
},
{
"category": "anti-social-behaviour",
"location_type": "Force",
"location": {
"latitude": "51.500440",
"street": {
"id": 953881,
"name": "On or near Chambers Street"
},
"longitude": "-0.066891"
},
"context": "",
"outcome_status": null,
"persistent_id": "",
"id": 53832881,
"location_subtype": "",
"month": "2016-12"
} ]
var categories = data.reduce(function(acc, crime) {
if (!acc[crime.category])
acc[crime.category] = 1;
else
acc[crime.category]++;
return acc;
}, {});
console.log(categories);
console.log(categories["Burglary"]);
Note that there's no such thing as a JSON object. Either you have JSON, which is a string representation/serialisation of your array/object, or you have an actual array or object.
Another approach, using Array#forEach.
var json = [{category:"Burglary",location_type:"Force",location:{latitude:"51.497877",street:{id:953834,name:"On or near Major Road"},longitude:"-0.064175"},context:"",outcome_status:null,persistent_id:"",id:53832838,location_subtype:"",month:"2016-12"},{category:"anti-social-behaviour",location_type:"Force",location:{latitude:"51.497877",street:{id:953834,name:"On or near Major Road"},longitude:"-0.064175"},context:"",outcome_status:null,persistent_id:"",id:53832841,location_subtype:"",month:"2016-12"},{category:"anti-social-behaviour",location_type:"Force",location:{latitude:"51.497877",street:{id:953834,name:"On or near Major Road"},longitude:"-0.064175"},context:"",outcome_status:null,persistent_id:"",id:53832849,location_subtype:"",month:"2016-12"},{category:"anti-social-behaviour",location_type:"Force",location:{latitude:"51.500440",street:{id:953881,name:"On or near Chambers Street"},longitude:"-0.066891"},context:"",outcome_status:null,persistent_id:"",id:53832881,location_subtype:"",month:"2016-12"}],
result = {};
json.forEach(function(v){
!result[v.category] ? result[v.category] = 1 : result[v.category] += 1;
});
console.log(result);
Use Array.prototype.reduce() function:
var data = [{category:"Burglary",location_type:"Force",location:{latitude:"51.497877",street:{id:953834,name:"On or near Major Road"},longitude:"-0.064175"},context:"",outcome_status:null,persistent_id:"",id:53832838,location_subtype:"",month:"2016-12"},{category:"anti-social-behaviour",location_type:"Force",location:{latitude:"51.497877",street:{id:953834,name:"On or near Major Road"},longitude:"-0.064175"},context:"",outcome_status:null,persistent_id:"",id:53832841,location_subtype:"",month:"2016-12"},{category:"anti-social-behaviour",location_type:"Force",location:{latitude:"51.497877",street:{id:953834,name:"On or near Major Road"},longitude:"-0.064175"},context:"",outcome_status:null,persistent_id:"",id:53832849,location_subtype:"",month:"2016-12"},{category:"anti-social-behaviour",location_type:"Force",location:{latitude:"51.500440",street:{id:953881,name:"On or near Chambers Street"},longitude:"-0.066891"},context:"",outcome_status:null,persistent_id:"",id:53832881,location_subtype:"",month:"2016-12"}],
counts = data.reduce(function (r, o) {
(r[o.category])? r[o.category]++ : r[o.category] = 1;
return r;
}, {});
console.log(counts);
You can do it with Array#reduce,
var arr = [{
"category": "Burglary",
}, {
"category": "anti-social-behaviour",
}, {
"category": "anti-social-behaviour",
}, {
"category": "anti-social-behaviour",
}];
var results = arr.reduce(function(result, itm){
return (result[itm.category] = (result[itm.category] || 0) + 1, result);
}, {});
console.log(results); //Object {Burglary: 1, anti-social-behaviour: 3}
DEMO
I have an array events as follows,
[{
"_id": "5890b4796166c457ffdee243",
"description": "Adele",
"name": "Adele",
"place": {
"name": "Houston Toyota Center",
"location": {
"city": "Houston",
"country": "United States",
"latitude": 29.751054939716,
"longitude": -95.362142762854,
"state": "TX",
"street": "1510 Polk St",
"zip": "77002",
"_id": "58992aebf2dbf4369c0a0325"
},
"id": "200866860532",
"_id": "5890b47c6166c457ffdee394"
},
"start_time": "2016-11-09T20:00:00-0600",
"id": "1644669702488073"
}, {
"_id": "5890b4796166c457ffdee242",
"description": "Please note that delivery will be delayed on all tickets until Friday January 8, 2016. Please adhere to the published ticket limits, which will be strictly enforced. If you exceed these limits, you may have any or all of your orders and tickets cancelled without notice. Please note: Every person, regardless of age, must have a ticket to be admitted to this event. RAIL RIDE EVENT: When you purchase a ticket to a Talking Stick Resort Arena event, you can ride the METRO LIGHT RAIL at no cost for four hours prior to the event through the end of the transit day.",
"name": "Adele",
"place": {
"name": "Talking Stick Resort Arena",
"location": {
"city": "Phoenix",
"country": "United States",
"latitude": 33.445995372225,
"longitude": -112.07135782626,
"state": "AZ",
"street": "201 E Jefferson St",
"zip": "85004",
"_id": "58992aebf2dbf4369c0a0327"
},
"id": "53475637527",
"_id": "5890b4856166c457ffdee427"
},
"start_time": "2016-11-21T19:30:00-0700",
"id": "905384112862937"
}, {
"_id": "5890b4796166c457ffdee24a",
"description": "Delivery of tickets will be delayed until 12/31/15",
"name": "Adele",
"place": {
"name": "AmericanAirlines Arena",
"location": {
"city": "Miami",
"country": "United States",
"latitude": 25.781236943411,
"longitude": -80.188316709574,
"state": "FL",
"street": "601 Biscayne Blvd",
"zip": "33132",
"_id": "58992aebf2dbf4369c0a0329"
},
"id": "120400119061",
"_id": "5890b4946166c457ffdee464"
},
"start_time": "2016-10-25T19:30:00-0400",
"id": "445046279020601"
}, {
"_id": "5890b4796166c457ffdee244",
"description": "Adele",
"name": "Adele",
"place": {
"name": "Houston Toyota Center",
"location": {
"city": "Houston",
"country": "United States",
"latitude": 29.751054939716,
"longitude": -95.362142762854,
"state": "TX",
"street": "1510 Polk St",
"zip": "77002",
"_id": "58992aebf2dbf4369c0a032b"
},
"id": "200866860532",
"_id": "5890b47c6166c457ffdee354"
},
"start_time": "2016-11-08T20:00:00-0600",
"id": "1662607760654203"
}, {
"_id": "5890b4796166c457ffdee245",
"description": "Delivery will be delayed until Oct 2, 2016.",
"name": "Adele",
"place": {
"name": "American Airlines Center",
"location": {
"city": "Dallas",
"country": "United States",
"latitude": 32.790485550848,
"longitude": -96.810278349053,
"state": "TX",
"street": "2500 Victory Ave",
"zip": "75219",
"_id": "58992aebf2dbf4369c0a032d"
},
"id": "26606856232",
"_id": "5890b47b6166c457ffdee2e4"
},
"start_time": "2016-11-02T20:00:00-0500",
"id": "649884741817020"
}]
How to get all the city from the above json using typescript?
i have tried this,
this.eventsFiltered = this.events.filter(
book => book.place.location.city);
try this:
this.eventsFiltered = this.events.map(
book => book.place.location.city);
EDIT
i want to filter the events which has speficic places?
let events = [{
"_id": "5890b4796166c457ffdee243",
"description": "Adele",
"name": "Adele",
"place": {
"name": "Houston Toyota Center",
"location": {
"city": "Houston",
"country": "United States",
"latitude": 29.751054939716,
"longitude": -95.362142762854,
"state": "TX",
"street": "1510 Polk St",
"zip": "77002",
"_id": "58992aebf2dbf4369c0a0325"
},
"id": "200866860532",
"_id": "5890b47c6166c457ffdee394"
},
"start_time": "2016-11-09T20:00:00-0600",
"id": "1644669702488073"
}, {
"_id": "5890b4796166c457ffdee242",
"description": "Please note that delivery will be delayed on all tickets until Friday January 8, 2016. Please adhere to the published ticket limits, which will be strictly enforced. If you exceed these limits, you may have any or all of your orders and tickets cancelled without notice. Please note: Every person, regardless of age, must have a ticket to be admitted to this event. RAIL RIDE EVENT: When you purchase a ticket to a Talking Stick Resort Arena event, you can ride the METRO LIGHT RAIL at no cost for four hours prior to the event through the end of the transit day.",
"name": "Adele",
"place": {
"name": "Talking Stick Resort Arena",
"location": {
"city": "Phoenix",
"country": "United States",
"latitude": 33.445995372225,
"longitude": -112.07135782626,
"state": "AZ",
"street": "201 E Jefferson St",
"zip": "85004",
"_id": "58992aebf2dbf4369c0a0327"
},
"id": "53475637527",
"_id": "5890b4856166c457ffdee427"
},
"start_time": "2016-11-21T19:30:00-0700",
"id": "905384112862937"
}, {
"_id": "5890b4796166c457ffdee24a",
"description": "Delivery of tickets will be delayed until 12/31/15",
"name": "Adele",
"place": {
"name": "AmericanAirlines Arena",
"location": {
"city": "Miami",
"country": "United States",
"latitude": 25.781236943411,
"longitude": -80.188316709574,
"state": "FL",
"street": "601 Biscayne Blvd",
"zip": "33132",
"_id": "58992aebf2dbf4369c0a0329"
},
"id": "120400119061",
"_id": "5890b4946166c457ffdee464"
},
"start_time": "2016-10-25T19:30:00-0400",
"id": "445046279020601"
}, {
"_id": "5890b4796166c457ffdee244",
"description": "Adele",
"name": "Adele",
"place": {
"name": "Houston Toyota Center",
"location": {
"city": "Houston",
"country": "United States",
"latitude": 29.751054939716,
"longitude": -95.362142762854,
"state": "TX",
"street": "1510 Polk St",
"zip": "77002",
"_id": "58992aebf2dbf4369c0a032b"
},
"id": "200866860532",
"_id": "5890b47c6166c457ffdee354"
},
"start_time": "2016-11-08T20:00:00-0600",
"id": "1662607760654203"
}, {
"_id": "5890b4796166c457ffdee245",
"description": "Delivery will be delayed until Oct 2, 2016.",
"name": "Adele",
"place": {
"name": "American Airlines Center",
"location": {
"city": "Dallas",
"country": "United States",
"latitude": 32.790485550848,
"longitude": -96.810278349053,
"state": "TX",
"street": "2500 Victory Ave",
"zip": "75219",
"_id": "58992aebf2dbf4369c0a032d"
},
"id": "26606856232",
"_id": "5890b47b6166c457ffdee2e4"
},
"start_time": "2016-11-02T20:00:00-0500",
"id": "649884741817020"
}];
let eventsFiltered = events.map(
book => book.place.location.city);
console.log(eventsFiltered);
let city = 'Houston';
let eventsCt = events.filter(book => book.place.location.city == city);
console.log('2nd question');
console.log(eventsCt);
EDIT 2
i have a field named "parent" which is an array of string. i already
have a string named "12344" which is a part of array parent. how do i
check if the events.parent cotains this string and filter those
objects
let p = '1234';
let eventsF = events.filter(book => book.parent && Array.isArray(book.parent)&& book.parent.indexOf(p) !== -1);
Filter used for filter records with where conditions. In your case you want transform data and you just want a array of city from an array of object. So in this case use map instance of filter
Example
this.cityList = this.events.map(book => book.place.location.city);
I have q requirement where I receive a JSON input and display it. Whenever I click on any value of the JSON, I need to get all parent keys in sequence.
Below is the sample JSON
{
"response": {
"Location": [{
"Name": "New York, NY",
"Point": {
"Latitude": "40.714550018310547",
"Longitude": "-74.007118225097656"
},
"BoundingBox": {
"SouthLatitude": "40.36376953125",
"WestLongitude": "-74.745925903320312",
"NorthLatitude": "41.056510925292969",
"EastLongitude": "-73.267219543457031"
},
"EntityType": "PopulatedPlace",
"Address": {
"AdminDistrict": "NY",
"CountryRegion": "United States",
"FormattedAddress": "New York, NY",
"Locality": "New York"
},
"Confidence": "High",
"MatchCode": "Good",
"GeocodePoint": {
"Latitude": "40.714550018310547",
"Longitude": "-74.007118225097656",
"CalculationMethod": "Rooftop",
"UsageType": "Display"
}
}, {
"Name": "New York, NY",
"Point": {
"Latitude": "40.714550018310547",
"Longitude": "-74.007118225097656"
},
"BoundingBox": {
"SouthLatitude": "40.36376953125",
"WestLongitude": "-74.745925903320312",
"NorthLatitude": "41.056510925292969",
"EastLongitude": "-73.267219543457031"
},
"EntityType": "PopulatedPlace",
"Address": {
"AdminDistrict": "NY",
"CountryRegion": "United States",
"FormattedAddress": "New York, NY",
"Locality": "New York"
},
"Confidence": "High",
"MatchCode": "Good",
"GeocodePoint": {
"Latitude": "40.714550018310547",
"Longitude": "-74.007118225097656",
"CalculationMethod": "Rooftop",
"UsageType": "Display"
}
}]
}
}
Now, I have this key chain in json response β Location β Point β Latitude. Suppose I have a key Latitude and I want all its parent keys in a sequence, how do I get that?
Note: This is a sample JSON. Keep in mind that I will get JSON dynamically.