简体   繁体   English

在Json中搜索并获得有限的搜索结果

[英]Search in Json and get limited search results

I've a JSON file which looks like this 我有一个看起来像这样的JSON文件

[{
    "id": "2",
    "word": "Aam",
    "type": "n.",
    "descr": " A Dutch and German measure of liquids, varying in different cities, being at Amsterdam about 41 wine gallons, at Antwerp 36 1\/2, at Hamburg 38 1\/4.",
    "track": "a",
    "track_2": "Aa",
    "track_3": "Aam"
}, {
    "id": "3",
    "word": "Aard-vark",
    "type": "n.",
    "descr": " An edentate mammal, of the genus Orycteropus, somewhat resembling a pig, common in some parts of Southern Africa. It burrows in the ground, and feeds entirely on ants, which it catches with its long, slimy tongue.",
    "track": "a",
    "track_2": "Aa",
    "track_3": "Aar"
}, {
    "id": "4",
    "word": "Aard-wolf",
    "type": "n.",
    "descr": " A carnivorous quadruped (Proteles Lalandii), of South Africa, resembling the fox and hyena. See Proteles.",
    "track": "a",
    "track_2": "Aa",
    "track_3": "Aar"
}, {
    "id": "5",
    "word": "Aaronic",
    "type": "a.",
    "descr": " Alt. of Aaronical",
    "track": "a",
    "track_2": "Aa",
    "track_3": "Aar"
}, {
    "id": "6",
    "word": "Aaronical",
    "type": "a.",
    "descr": " Pertaining to Aaron, the first high priest of the Jews.",
    "track": "a",
    "track_2": "Aa",
    "track_3": "Aar"
}, {
    "id": "7",
    "word": "Aarons rod",
    "type": "",
    "descr": " A rod with one serpent twined around it, thus differing from the caduceus of Mercury, which has two.",
    "track": "a",
    "track_2": "Aa",
    "track_3": "Aar"
}, {
    "id": "8",
    "word": "Aarons rod",
    "type": "",
    "descr": " A plant with a tall flowering stem; esp. the great mullein, or hag-taper, and the golden-rod.",
    "track": "a",
    "track_2": "Aa",
    "track_3": "Aar"
}]

I want get first 2 or 3 result's by where word = "Aa" , track_2 = "aa" , track_3 = "aar" .. how can i do that ? 我想通过单词=“ Aa”,track_2 =“ aa”,track_3 =“ aar”来获得前2或3个结果。我该怎么做?

What I need 我需要的
1. Query in Json by String 2. Get Limited results 3. I want to search in the json file by 2-3 conditions 1.通过字符串在Json中查询2.获得有限的结果3.我想按2-3个条件在json文件中搜索

i'm trying this 我正在尝试这个

$(window).load(function() {

    $('#search').keyup(function() {
        var searchField = $('#search').val();

        $.getJSON('files/aa.json', function(data) {

            var jsonArrr = data;
            var matchMe = new RegExp('^' + searchField, 'i');
            var matches = [];

            for (var i in jsonArrr) {
                if (jsonArrr[i].word.search(matchMe) > -1) {
                    matches.push({
                        'id': jsonArrr[i].word,
                        'word': jsonArrr[i].word
                    });
                }
            }

            for (var i in matches) {
                console.log(matches[i].word);
            }
        });
    });
});

Is there a way to get what I need by expanding some code ? 有没有办法通过扩展一些代码来获得所需的信息?

add few lines in this part - 在这一部分中添加几行-

var matches = [];

for (var i in jsonArrr) {

    if ( jsonArrr[i].word.search(matchMe) > -1 && jsonArrr[i].type.search(matchType) > -1 ) { // define matchType for the type keyword

            matches.push( {'id': jsonArrr[i].word , 'word': jsonArrr[i].word} );

    }

if(matches.length == 2) { // replace 2 with your number of results - 1
break;
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM