Reputation: 41
hello im a newbie in javascript , I need to check if the Object value is listed in the JSON or not. If the yes do something else do another thing.
first i get JSON from ipapi.co/json which return this for example
Thanks Problem Solved!
Upvotes: 0
Views: 104
Reputation: 20934
Arrays in JavaScript have some in-build methods which you can do specific tasks with, like the some
method in which you can check if one or more parts of the ISP names are partly in the list. It will return true
or false
which you can use to determine the text of your $("#ip")
element.
$.getJSON("https://ipapi.co/json/", function (data) {
// Create a list of organizations.
var organizations = ["republik","telekomunikasi","fastnet","indosatm2","telekomunikasi","indosat","cyberindo","biznet","xl","telematika","hutchison","smartfren","mnc","wireless indonesia","antar nusa","biznet","ezecom","win","axis","linknet-fastnet","indonesia","bali","linknet","indosat","antar nusa","indo","firstmedia","s.i","cambodia","neuviz"];
// Lowercased version of the ISP.
var isp = data.org.toLowerCase();
// Check if the lowercased ISP occurs in any of the organizations.
var orgExists = organizations.some(org => org.includes(isp));
// Ternary if statement.
// If orgExists === true, text will be "exist: " + isp.
// Otherwise text will be "no exist".
var text = orgExists ? "exist: " + isp : "no exist";
// Output the text.
$("#ip").text(text);
});
Upvotes: 0
Reputation: 3874
You can combine basic array/object functions to archive your goal. But as i commented on your question, you should define strict filter criterias.
const data = {
"ip": "103.26.208.254",
"version": "IPv4",
"city": "Jakarta",
"region": "Jakarta",
"region_code": "JK",
"country": "ID",
"country_name": "Indonesia",
"country_code": "ID",
"country_code_iso3": "IDN",
"country_capital": "Jakarta",
"country_tld": ".id",
"continent_code": "AS",
"in_eu": false,
"postal": null,
"latitude": -6.1741,
"longitude": 106.8296,
"timezone": "Asia/Jakarta",
"utc_offset": "+0700",
"country_calling_code": "+62",
"currency": "IDR",
"currency_name": "Rupiah",
"languages": "id,en,nl,jv",
"country_area": 1919440.0,
"country_population": 267663435.0,
"asn": "AS18103",
"org": "Neuviz Net"
};
// create a regex of each filter value
const filters = [
"republik",
"telekomunikasi",
"fastnet", "indosatm2",
"telekomunikasi", "indosat",
"cyberindo", "biznet", "xl",
"telematika", "hutchison",
"smartfren", "mnc",
"wireless indonesia",
"antar nusa", "biznet",
"ezecom", "win", "axis",
"linknet-fastnet", "indonesia",
"bali", "linknet", "indosat",
"antar nusa", "indo", "firstmedia",
"s.i", "cambodia", "neuviz"
].map((v) => {
return new RegExp(`${v}`, "gi");
});
// get array of values from object
// filter array based on the test criteria
let matches = Object.values(data).filter((v) => {
// conver every value to astring
let value = String(v).toLowerCase();
// test each filter on value
return filters.some((filter) => {
// test the regular expression on the value
return filter.test(value);
});
});
if (matches.length > 0) {
console.log("Filter matched!", matches);
} else {
console.log("Filte *not* matched")
}
The code is well documented, if you dont understand a snippet/chunk, let me know. If it dosnt work like expected, let me know too!
Happy coding!
Upvotes: 1