获取字符串

时间:2017-02-18 21:49:36

标签: javascript

我有一个带有一些域的json文件,我尝试在foreach对象函数中匹配。

JSON-文件:

  "calendar-google": {
    "domainMatch": "calendar.google",
    "icon": "assets/icons/google-calendar.png"
  },
  "inbox-google": {
    "domainMatch": "inbox.google",
    "icon": "assets/icons/google-gmail.png"
  },
  "github": {
    "domainMatch": "github",
    "icon": "assets/icons/github.png"
  },
  "google": {
    "domainMatch": "google",
    "icon": "assets/icons/google-google.png"
  },
  "trello": {
    "domainMatch": "trello",
    "icon": "assets/icons/trello.png"
  },
  "tweetdeck": {
    "domainMatch": "tweetdeck.twitter",
    "icon": "assets/icons/twitter.png"
  },
  "twitter": {
    "domainMatch": "twitter",
    "icon": "assets/icons/twitter.png"
  },
  "youtube": {
    "domainMatch": "youtube",
    "icon": "assets/icons/youtube.png"
  }

现在我想检查我本地存储中的网址是否与其中一个网址匹配" domainMatch"属性。

JavaScript的:

$.getJSON("domainList.json", function  (data) {
        Object.getOwnPropertyNames(data).forEach(function(val, idx, array) {

            var domainMatch = data[val].domainMatch

            if(localStorage.getItem("favouriteSite")) {
                var sites = JSON.parse(localStorage.getItem("favouriteSite"));

                // Lets think firstlink is "calender.google.com"

                var firstLink = sites.site1.link;

                if(firstLink.includes(domainMatch)){
                    // 2 Results but I want only that
                    // result which is near!!     
                }

            }
        });

您在评论中看到我不想要匹配" google"和" calender.google"。我只想要两者中最接近的匹配(在这种情况下为calender.google)。

如何获得最接近的字符串匹配?

当我没有写出足够详细的内容时,请写下来,但我认为你应该理解我的意思。

问候 儒略

2 个答案:

答案 0 :(得分:0)

而不是forEach,使用find获取第一个匹配,如果没有匹配,请使用null

$.getJSON("domainList.json", function(data) {
  var sites = JSON.parse(localStorage.getItem("favouriteSite")); // this should be out here (so you won't fetch it evertime)
  var firstLink = sites.site1.link;

  var val = Object.keys(data).find(function(key) { // find will stop at the first found item (Object.keys) is better since it's just a plain object)
    var domainMatch = data[key].domainMatch;

    return firstLink.includes(domainMatch);
  });
  console.log(val); // val will be the first matched key ('github', 'google', ...) or null if nothing is matched


  /***************** EDIT: **********************/
  if(val) {                // if we found something
      var obj = data[val]; // get the object (because val is the key)
      var icon = obj.icon; // now obj is an object from the array data (you can access its icon property like this)
  }
});

答案 1 :(得分:0)

使用Array.prototype.some()函数立即在第一场比赛中返回结果:

$.getJSON("domainList.json", function  (data) {
    var matched = Object.keys(data).some(function(k) {
        var domainMatch = data[k].domainMatch;

        if (localStorage.getItem("favouriteSite")) {
            var sites = JSON.parse(localStorage.getItem("favouriteSite"));

            // Lets think firstlink is "calender.google.com"
            var firstLink = sites.site1.link;

            return firstLink.includes(domainMatch);
        }
    };
});