Related
I am trying to figure out a way to filter out top hierarchical elements from an array, where each object may have a parent-child hierarchy with other elements of the array. Here is what the array looks like:
[
{
"Code": "1A",
"name": "Western Europe",
"hierarchy": "World/",
},
{
"Code": "AT",
"name": "Austria",
"hierarchy": "World/ Western Europe/"
},
{
"Code": "NL",
"name": "Netherlands",
"hierarchy": "World/ Western Europe/"
}
]
For this example, the output should be only the first object, because Western Europe is the parent of Austria and the Netherlands.
[
{
"Code": "WO",
"name": "World",
"hierarchy": "",
},
{
"Code": "NA",
"name": "North America",
"hierarchy": "World/",
},
{
"Code": "NL",
"name": "Netherlands",
"hierarchy": "World/ Western Europe/"
}
]
Here the output will be World because World is the top Hierarchy.
[
{
"Code": "1A",
"name": "Western Europe",
"hierarchy": "World/",
},
{
"Code": "NA",
"name": "North America",
"hierarchy": "World/",
},
{
"Code": "US",
"name": "United States",
"hierarchy": "World/ North America/",
},
{
"Code": "NL",
"name": "Netherlands",
"hierarchy": "World/ Western Europe/"
},
{
"Code": "RS",
"name": "Russia",
"hierarchy": "World/ Eastern Europe/",
},
]
Here, the output should be Western Europe, North America, and Russia (As we don't have Eastern Europe in the list so Russia is at the top of its hierarchy). So, We have to get the top hierarchical elements. I have a function where I can pass the Code and get the hierarchy structure:
getParentChildHierarchy(string Code) { //some code here }
Input: AT
Output: WO>1A>AT
Can you suggest an optimum way to solve this?
One way is to create a Set of all the paths in the data set, i.e. the concatenation of hierarchy with the name (following the same format with slashes and spacing). Then check for which nodes the hierarchy (which references a parent) actually exists in that set. If not, it does not have a parent in the data set:
const data = [
{
"Code": "1A",
"name": "Western Europe",
"hierarchy": "World/",
}, {
"Code": "NA",
"name": "North America",
"hierarchy": "World/",
}, {
"Code": "US",
"name": "United States",
"hierarchy": "World/ North America/",
}, {
"Code": "NL",
"name": "Netherlands",
"hierarchy": "World/ Western Europe/"
}, {
"Code": "RS",
"name": "Russia",
"hierarchy": "World/ Eastern Europe/",
},
];
const keys = new Set(data.map(o => o.hierarchy + " " + o.name + "/"));
const topLevel = data.filter(o => !keys.has(o.hierarchy));
console.log(topLevel);
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'm trying to iterate through a large list of financial institutions (condensed from the 8974 objects to only 2 below) using javascript.
I want to save "id" and "name" in a new list. I'm a beginner at this and after trying some code in w3 website was unable to iterate through the different object capturing multiple "id"s and "name"s.
Anyone have an idea how to accomplish iterating through and capturing every
"id" and "name" in each json object within the json array?
{
"found": 8974,
"displaying": 8974,
"moreAvailable": false,
"createdDate": 1550566839,
"institutions": [
{
"id": 5,
"name": "Chase",
"accountTypeDescription": "Banking",
"phone": "1-800-242-7324",
"urlHomeApp": "https://www.chase.com/",
"urlLogonApp": "https://chaseonline.chase.com/chaseonline/logon/sso_logon.jsp",
"oauthEnabled": false,
"urlForgotPassword": "",
"urlOnlineRegistration": "",
"institutionClass": "banking",
"tpCurrencyCode": "USD",
"specialText": "Please enter your Chase User ID and Password. ",
"emailAddress": "https://www.bankone.com/contactus/#personal",
"address": {
"addressLine1": "270 Park Avenue",
"addressLine2": "270 Park Avenue, New York",
"city": "New York",
"country": "USA",
"postalCode": "10017",
"state": "NY"
}
},
{
"id": 170703,
"name": "WWW Bank",
"accountTypeDescription": "TestFI",
"phone": "21210",
"urlHomeApp": "http://www.finbank.com",
"urlLogonApp": "http://www.finbank.com",
"oauthEnabled": false,
"urlForgotPassword": "",
"urlOnlineRegistration": "",
"institutionClass": "testfi",
"tpCurrencyCode": "USD",
"specialText": "Please enter your WWW Bank User and Password required for login.",
"emailAddress": "finbank#finicity.com",
"address": {
"addressLine1": "Utah",
"addressLine2": "Utah",
"city": "Utah",
"country": "USA",
"postalCode": "",
"state": ""
}
}
]
}
Here we use a simnple map function to get desired array.
let data = {
"found": 8974,
"displaying": 8974,
"moreAvailable": false,
"createdDate": 1550566839,
"institutions": [
{
"id": 5,
"name": "Chase",
"accountTypeDescription": "Banking",
"phone": "1-800-242-7324",
"urlHomeApp": "https://www.chase.com/",
"urlLogonApp": "https://chaseonline.chase.com/chaseonline/logon/sso_logon.jsp",
"oauthEnabled": false,
"urlForgotPassword": "",
"urlOnlineRegistration": "",
"institutionClass": "banking",
"tpCurrencyCode": "USD",
"specialText": "Please enter your Chase User ID and Password. ",
"emailAddress": "https://www.bankone.com/contactus/#personal",
"address": {
"addressLine1": "270 Park Avenue",
"addressLine2": "270 Park Avenue, New York",
"city": "New York",
"country": "USA",
"postalCode": "10017",
"state": "NY"
}
},
{
"id": 170703,
"name": "WWW Bank",
"accountTypeDescription": "TestFI",
"phone": "21210",
"urlHomeApp": "http://www.finbank.com",
"urlLogonApp": "http://www.finbank.com",
"oauthEnabled": false,
"urlForgotPassword": "",
"urlOnlineRegistration": "",
"institutionClass": "testfi",
"tpCurrencyCode": "USD",
"specialText": "Please enter your WWW Bank User and Password required for login.",
"emailAddress": "finbank#finicity.com",
"address": {
"addressLine1": "Utah",
"addressLine2": "Utah",
"city": "Utah",
"country": "USA",
"postalCode": "",
"state": ""
}
}
]
};
let newArr = data.institutions.map(i => {
return {id: i.id, name: i.name }
});
console.log(newArr);
I have a requirement where I need to send modified value alone in the object.
Following is my object:
{
"Code": 200,
"ErrorMessage": null,
"Result": {
"Locations": [{
"LocationName": "Location 1",
"Address": "XYZ",
"City": "Houston",
"State": "TEXAS",
"StateCode": "TX",
"Zipcode": "75201"
},
{
"LocationName": "Location 2",
"Address": "ABC",
"City": "Germantown",
"State": "CALIFORNIA",
"StateCode": "CA",
"Zipcode": "90001"
}]
}
}
I used ng-repeat inorder to display data which has input fields. Now If I modify Location 1 in that Locations Object. I want to send only Location 1 details.
Is it possible to do that in Angular. I am new to angular.
you can use ng-change to get the modified object
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.changeItem = function(item){
console.log(item.LocationName)
}
$scope.items = {
"Code": 200,
"ErrorMessage": null,
"Result": {
"Locations": [{
"LocationName": "Location 1",
"Address": "XYZ",
"City": "Houston",
"State": "TEXAS",
"StateCode": "TX",
"Zipcode": "75201"
},
{
"LocationName": "Location 2",
"Address": "ABC",
"City": "Germantown",
"State": "CALIFORNIA",
"StateCode": "CA",
"Zipcode": "90001"
}]
}
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div ng-repeat="item in items.Result.Locations">
<input ng-model="item.LocationName" ng-change="changeItem(item)"/>
</div>
</div>
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);