2017-02-18 4 views
2

私はforeachオブジェクト関数で一致させようとするいくつかのドメインを持つjsonファイルを持っています。文字列に最も近いものを取得

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

今、私は私のローカルストレージでの私のURLがこれらの「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!!  
       } 

      } 
     }); 

あなたは、私は試合は "グーグル" と "calender.google" 望んでいないことを、コメントでご覧ください。私は両方から最も近い一致だけを必要とします(この場合はcalender.google)。

文字列に最も近い一致を得るにはどうすればよいですか?

私が何か詳細を書いていないときは、それを書いてください。私はあなたが私が何を意味するのか理解していると思います。

挨拶 ジュリアン

+1

'.includes()'関数はブール値VAを返します。 lue - 'true'または' false'です。では、どうやって2つの結果が得られるのでしょうか? –

+0

文字列を使用すると、正確に一致しているかどうか、または指定された文字列が別の文字列内にあるかどうかを確認できます。 「最も近いもの」のための組み込みのメカニズムはありません。あなたはそれのための独自のアルゴリズムを書く必要があります。 –

+0

@Kinduser誤解をおかけして申し訳ありません。私がすべての試合を記録すると、「真」が2回現れます。 – julianYaman

答えて

0

代わりのforEach、何もこのようにマッチしていない場合は、最初に一致またはnullを取得するためにfindを使用します。最初の試合ですぐに結果を返すために

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

これで、返されるオブジェクトのiconプロパティにアクセスしたいと思います。どうやってやるの? – julianYaman

+0

@julianYamanコードの** EDIT **を参照してください! –

+0

ありがとう、それは動作します!とにかく少し質問があります。 jsonファイルの末尾に_calender-google_オブジェクトを設定すると、カレンダーのオブジェクトではなく_go​​ogle_オブジェクトが返されます。 _calender-google_オブジェクトは_google_オブジェクトプロパティの上に設定する必要があります。それも "修正"する解決策はありますか? – julianYaman

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); 
     } 
    }; 
}); 
+0

いくつかは真または偽を返し、関数によって返された値を返しません。私はあなたが '還元 'を探していると思う。 –

+0

私が認識できる主なアイデアはOPのものでした*自分のローカルストレージ内のURLがこれらの "domainMatch"のいずれかと一致するかどうか確認したい* – RomanPerekhrest

関連する問題