搜索字典内的键和打印值 - Javascript

时间:2017-07-04 14:41:21

标签: javascript json dictionary

我有一本看起来像这样的字典:

var index = {
  "Italian": [
    {
      "name": "Il Brigante",
      "rating": "5.0"
    },
    {
      "name": "Giardino Doro Ristorante",
      "rating": "5.0"
    }
  ],
  "Mexican": [
    {
      "name": "Cosme",
      "rating": "5.0"
    }
  ]
}

我还有一个人们可以输入查询的搜索栏。我想要做的是获取查询并在index内搜索它。

示例:search = "italian",results = {"name": "Il Brigante", "rating": "5.0"}, {"name": "Giardino Doro Ristorante", "rating": "5.0"}

这种搜索可能吗?任何帮助,将不胜感激。非常感谢提前。快乐第四!

干杯, 狄奥

4 个答案:

答案 0 :(得分:2)

您可以尝试以下



var index = {
  "Italian": [{
      "name": "Il Brigante",
      "rating": "5.0"
    },
    {
      "name": "Giardino Doro Ristorante",
      "rating": "5.0"
    }
  ],
  "Mexican": [{
    "name": "Cosme",
    "rating": "5.0"
  }]
};

var search = "italian";


var results = Object.keys(index).reduce(function(a, b) {
  if (b.toLowerCase() === search.toLowerCase()) {
    return a.concat(index[b]);
  }
  return a;
}, []);

console.log(results);




以后可能是一个要求!

如果你想要部分匹配,你只需要替换

if (b.toLowerCase() === search.toLowerCase()) {

if(b.toLowerCase().indexOf(search.toLowerCase()) !== -1) {

进行调整 - plunker

答案 1 :(得分:0)

您可以直接使用hasOwnProperty,但这只会返回完全匹配。最简单的方法是遍历索引属性并查找匹配项并返回结果值。

var index = {
  "Italian": [
    {
      "name": "Il Brigante",
      "rating": "5.0"
    },
    {
      "name": "Giardino Doro Ristorante",
      "rating": "5.0"
    }
  ],
  "Mexican": [
    {
      "name": "Cosme",
      "rating": "5.0"
    }
  ]
}

console.log(index.hasOwnProperty('italian'));
console.log(index.hasOwnProperty('Italian'));
console.log(dictionarySearch('Italian', index));

function dictionarySearch(term, dictionary)
{
  var found = false;
  if (typeof dictionary === 'object') {
    for(property in dictionary) {
      if (dictionary.hasOwnProperty(property)) {
        if (property.toLowerCase() === term.toLowerCase()) {
          found = dictionary[property];
        }
      }
    }
  }

  return found;
}

答案 2 :(得分:0)

查看方括号表示法。

  

当动态确定属性名称时(直到运行时才确定属性名称),此表示法也非常有用。

Source with examples

将此问题应用于您的问题:

var index = {
  "Italian": [
    {
      "name": "Il Brigante",
      "rating": "5.0"
    },
    {
      "name": "Giardino Doro Ristorante",
      "rating": "5.0"
    }
  ],
  "Mexican": [
    {
      "name": "Cosme",
      "rating": "5.0"
    }
  ]
}

var query = "Italian"
var result = index[query]

console.log(index["Italian"])

答案 3 :(得分:0)

我认为当我们有像'itAlIaN'/'italian'/'ITALIAN'这样的索引时没有任何情况所以索引是唯一的,无论你是否有大写/小写

var index = {
    "Italian": [
        {
            "name": "Il Brigante",
            "rating": "5.0"
        },
        {
            "name": "Giardino Doro Ristorante",
            "rating": "5.0"
        }
    ],
    "Mexican": [
        {
            "name": "Cosme",
            "rating": "5.0"
        }
    ]
}

// function to normalize index by having keys lowercase
function lowercaseIndex (obj) {
    var keys = Object.keys(obj);
    var n = keys.length;
    var normalized = {};
    while (n--) {
        var key = keys[n];
        normalized[key.toLowerCase()] = obj[key];
    }

    return normalized;
}

index = lowercaseIndex(index);

function search(needle) {
    return index[needle.toLowerCase()] || "No results";
}

// tests
console.log(search('Italian')); // output: [{"name":"Il Brigante","rating":"5.0"},{"name":"Giardino Doro Ristorante","rating":"5.0"}]
console.log(search('italian')); // output: [{"name":"Il Brigante","rating":"5.0"},{"name":"Giardino Doro Ristorante","rating":"5.0"}]
console.log(search('iTaLiAn')); // output: [{"name":"Il Brigante","rating":"5.0"},{"name":"Giardino Doro Ristorante","rating":"5.0"}]
console.log(search('mexican')); // output: [{"name":"Cosme","rating":"5.0"}]
console.log(search('English')); // output: "No results"