0

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!

3
  • 2
    You need/should define "strict"/exact filter criterias. To match "Neuviz Net" & "neuviz" is not a good solution and screams for false positiv results Commented Nov 5, 2020 at 9:58
  • What's the point of stringifying the live objects? It would be extremely hard to get the wanted values out of the strings. Commented Nov 5, 2020 at 10:08
  • @Teemu i gues it was a attempt for a simple == "filter", but your right. Would be difficult so. Commented Nov 5, 2020 at 10:16

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

4 Comments

anyway , i think this "const / let" doesnt work well with android app, because im trying to build android app webview and its not working , but on browser its work perfectly..
@KuroyukiHime You didnt mention that anywhere, but feel free to change this to var or whatever you like.
can you show me how ? because im not really good at javascript .. thanks for your help.
As i have written, just use var instead of const / let...
0

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

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.