Javascript get users location(country, lang, timezone) - javascript

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

Related

After placing a shipment using FedEx Sandbox APIs, I'm able to get the tracking number from response, But unable to track with that tracking number

I'm using the following FedEx sandbox API
https://apis-sandbox.fedex.com/ship/v1/shipments
and able to get the response which contains the tracking number.
But when I'm using that tracking number in the
https://apis-sandbox.fedex.com/track/v1/trackingnumbers
API getting the following error as TRACKING.TRACKINGNUMBER.NOTFOUND.
{
"transactionId": "b06f2eb2-8cdf-4139-83b7-1a6eccffed56",
"output": {
"completeTrackResults": [
{
"trackingNumber": "794693611559",
"trackResults": [
{
"trackingNumberInfo": {
"trackingNumber": "794693611559",
"trackingNumberUniqueId": "",
"carrierCode": ""
},
"error": {
"code": "TRACKING.TRACKINGNUMBER.NOTFOUND",
"message": "Tracking number cannot be found. Please correct the tracking number and try again."
}
}
]
}
]
}
}
But when I'm using the Mock tracking numbers provided by the FedEx for testing able to get the following response.
{
"transactionId": "3b56ae53-83fa-47f5-83d6-ec1ca6aeff3a",
"output": {
"completeTrackResults": [
{
"trackingNumber": "449044304137821",
"trackResults": [
{
"trackingNumberInfo": {
"trackingNumber": "449044304137821",
"trackingNumberUniqueId": "12013~449044304137821~FDEG",
"carrierCode": "FDXG"
},
"additionalTrackingInfo": {
"nickname": "",
"packageIdentifiers": [
{
"type": "GROUND_SHIPMENT_ID",
"values": [
"DMWsGWdnN"
],
"trackingNumberUniqueId": "",
"carrierCode": ""
},
{
"type": "CUSTOMER_REFERENCE",
"values": [
"115380173"
],
"trackingNumberUniqueId": "",
"carrierCode": ""
}
],
"hasAssociatedShipments": false
},
"shipperInformation": {
"address": {
"city": "JEFFERSONVILLE",
"stateOrProvinceCode": "IN",
"countryCode": "US",
"residential": false,
"countryName": "United States"
}
},
"recipientInformation": {
"address": {
"city": "Miami",
"stateOrProvinceCode": "FL",
"countryCode": "US",
"residential": false,
"countryName": "United States"
}
},
"latestStatusDetail": {
"code": "OC",
"derivedCode": "IN",
"statusByLocale": "Initiated",
"description": "Shipment information sent to FedEx",
"scanLocation": {
"city": "Miami",
"stateOrProvinceCode": "FL",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"ancillaryDetails": [
{
"reason": "IN001",
"reasonDescription": "Please check back later for shipment status or subscribe for e-mail notifications",
"action": "",
"actionDescription": ""
}
]
},
"dateAndTimes": [
{
"type": "ACTUAL_PICKUP",
"dateTime": "2016-08-01T00:00:00-06:00"
},
{
"type": "SHIP",
"dateTime": "2020-08-15T00:00:00-06:00"
}
],
"availableImages": [],
"packageDetails": {
"packagingDescription": {
"type": "YOUR_PACKAGING",
"description": "Package"
},
"physicalPackagingType": "PACKAGE",
"sequenceNumber": "1",
"count": "1",
"weightAndDimensions": {
"weight": [
{
"value": "3.0",
"unit": "LB"
},
{
"value": "1.36",
"unit": "KG"
}
],
"dimensions": [
{
"length": 14,
"width": 11,
"height": 5,
"units": "IN"
},
{
"length": 35,
"width": 27,
"height": 12,
"units": "CM"
}
]
},
"packageContent": []
},
"shipmentDetails": {
"possessionStatus": true
},
"scanEvents": [
{
"date": "2013-12-30T13:24:00-05:00",
"eventType": "OC",
"eventDescription": "Shipment information sent to FedEx",
"exceptionCode": "",
"exceptionDescription": "",
"scanLocation": {
"streetLines": [
""
],
"postalCode": "471307761",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"locationType": "CUSTOMER",
"derivedStatusCode": "IN",
"derivedStatus": "Initiated"
}
],
"availableNotifications": [],
"deliveryDetails": {
"deliveryAttempts": "0",
"deliveryOptionEligibilityDetails": [
{
"option": "INDIRECT_SIGNATURE_RELEASE",
"eligibility": "INELIGIBLE"
},
{
"option": "REDIRECT_TO_HOLD_AT_LOCATION",
"eligibility": "INELIGIBLE"
},
{
"option": "REROUTE",
"eligibility": "INELIGIBLE"
},
{
"option": "RESCHEDULE",
"eligibility": "INELIGIBLE"
},
{
"option": "RETURN_TO_SHIPPER",
"eligibility": "INELIGIBLE"
},
{
"option": "DISPUTE_DELIVERY",
"eligibility": "INELIGIBLE"
},
{
"option": "SUPPLEMENT_ADDRESS",
"eligibility": "INELIGIBLE"
}
],
"destinationServiceArea": "OC"
},
"originLocation": {
"locationContactAndAddress": {
"address": {
"city": "JEFFERSONVILLE",
"stateOrProvinceCode": "IN",
"countryCode": "US",
"residential": false,
"countryName": "United States"
}
}
},
"lastUpdatedDestinationAddress": {
"city": "Miami",
"stateOrProvinceCode": "FL",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"serviceCommitMessage": {
"message": "The delivery date may be updated when FedEx receives the package.",
"type": "SHIPMENT_LABEL_CREATED"
},
"serviceDetail": {
"type": "GROUND_HOME_DELIVERY",
"description": "FedEx Home Delivery",
"shortDescription": "HD"
},
"standardTransitTimeWindow": {
"window": {
"ends": "2014-01-02T00:00:00-06:00"
}
},
"estimatedDeliveryTimeWindow": {
"window": {}
},
"goodsClassificationCode": "",
"returnDetail": {}
}
]
}
]
}
}
Please help me with this, Thanks in advance.
Have you tried with a different real tracking number? Sometimes after sending a package, it takes a couple of hours for couriers such as FedEx and others update the tracking number...

How to compare object properties between different objects in different arrays

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

Filter data from object inside array typescript

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

How to get Parent keys in JSON

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.

Reading JSON file into javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
The last three hours i have been trying to read a JSON file into java-script for my map corodinates to be read by google-maps service.
I however,have an incomplete script..i can't get my results.Here is my script:
<script>
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'http://127.0.0.1/irismaps/aitems.php', true); // path to file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
function init() {
loadJSON(function(response) {
// Parse JSON string into object
results = JSON.parse(response);
for (var i=0; i<results[0].markers.length; i++) {
for (var b=0;b<results[0].markers[i].length;b++) {
//this is the object you are looking for
console.log(results[0].markers[i]);
break;
}
}
});
}
</script>
JSON:
{
"results": [
{
"markers": [
{
"longitude": "37.66653612499999",
"latitude": "55.77875861131171",
"title": "Industry LLC",
"icon": "img/markers/shop.png"
},
{
"longitude": "-82.28813722500001",
"latitude": "23.10569357133444",
"title": "Garden Care",
"icon": "img/markers/shop.png"
},
{
"longitude": "-4.792105912500006",
"latitude": "37.875266800953554",
"title": "Entertainers",
"icon": "img/markers/shop.png"
},
{
"longitude": "-73.94999999999999",
"latitude": "40.65",
"title": "Educators Corp.",
"icon": "img/markers/shop.png"
},
{
"longitude": "-0.8122895000000199",
"latitude": "52.0443456",
"title": "Rentacar",
"icon": "img/markers/shop.png"
},
{
"longitude": "4.879416200000037",
"latitude": "45.7783975",
"title": "Restaurant Lucia",
"icon": "img/markers/shop.png"
},
{
"longitude": "12.481953799999928",
"latitude": "41.90567180000001",
"title": "The Airport",
"icon": "img/markers/shop.png"
},
{
"longitude": "-74.0059731",
"latitude": "40.7143528",
"title": "Advertising Inc.",
"icon": "img/markers/shop.png"
},
{
"longitude": "12.7880859375",
"latitude": "4.565473550710291",
"title": "Car Repairs",
"icon": "img/markers/shop.png"
},
{
"longitude": "13.447265625",
"latitude": "52.53627304145948",
"title": "Accounting Gmbh.",
"icon": "img/markers/shop.png"
},
{
"longitude": "39.638671875",
"latitude": "24.447149589730845",
"title": "Courier & Courier",
"icon": "img/markers/shop.png"
},
{
"longitude": "10.7666015625",
"latitude": "59.93300042374631",
"title": "Your House A.S.",
"icon": "img/markers/shop.png"
},
{
"longitude": "-1.8896484375",
"latitude": "52.482780222078205",
"title": "M Supermarkets LLC",
"icon": "img/markers/shop.png"
},
{
"longitude": "21.005859375",
"latitude": "52.22779941887071",
"title": "Healthy Runner S.A.",
"icon": "img/markers/shop.png"
},
{
"longitude": "-0.8122895000000199",
"latitude": "52.0443456",
"title": "Rentacar",
"icon": "img/markers/shop.png"
},
{
"longitude": "-0.8122895000000199",
"latitude": "52.0443456",
"title": "Restaurant Lucia",
"icon": "img/markers/shop.png"
},
{
"longitude": "37.6173",
"latitude": "55.755826",
"title": "Industrie LLC",
"icon": "img/markers/shop.png"
},
{
"longitude": "-115.20111025",
"latitude": "55.80752971122906",
"title": "Book Group Inc.",
"icon": "img/markers/shop.png"
}
]
}
]
}
Try:
results.results[0].markers[i]
You are adding the entire file to an object called results that contains an array called results.
That JSON file you're trying to load is basically just JavaScript. Give your object a name like
var myJSONObject = {
"foo": "bar"
};
Save it as a .js file and load it into your document.

Categories