【问题标题】:How to check if the JSON Object array contains the value defined or not?如何检查 JSON 对象数组是否包含定义的值?
【发布时间】:2020-11-05 09:50:48
【问题描述】:

你好,我是 javascript 的新手, 我需要检查 JSON 中是否列出了 Object 值。如果是,则做其他事情。

首先我从 ipapi.co/json 获取 JSON,例如返回这个

感谢问题解决了!

【问题讨论】:

  • 您需要/应该定义“严格”/精确的过滤条件。匹配“Neuviz Net”和“neuviz”不是一个好的解决方案,并且会为假阳性结果尖叫
  • 对活动对象进行字符串化有什么意义?从字符串中获取想要的值是非常困难的。
  • @Teemu 我猜这是一个简单的==“过滤器”的尝试,但你是对的。会很困难。

标签: javascript jquery arrays json


【解决方案1】:

您可以结合基本的数组/对象函数来归档您的目标。 但是正如我对您的问题的评论,您应该定义严格的过滤条件。

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

}

代码有据可查,如果您不理解 sn-p/chunk,请告诉我。 如果它没有按预期工作,请告诉我!

编码愉快!

【讨论】:

  • 无论如何,我认为这个“const / let”不适用于 android 应用程序,因为我试图构建 android 应用程序 webview 并且它不工作,但在浏览器上它工作完美..
  • @KuroyukiHime 你没有在任何地方提到过,但请随意将其更改为 var 或任何你喜欢的。
  • 你能告诉我怎么做吗?因为我不太擅长 javascript .. 谢谢你的帮助。
  • 正如我所写,只需使用var 而不是const / let...
【解决方案2】:

JavaScript 中的数组有一些内置方法,您可以使用这些方法执行特定任务,例如 some 方法,您可以在其中检查 ISP 名称的一个或多个部分是否部分在列表中。它将返回truefalse,您可以使用它们来确定$("#ip") 元素的文本。

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

});

【讨论】:

    猜你喜欢
    • 2018-04-25
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 2019-01-12
    相关资源
    最近更新 更多