使用索引名称在JSON数组中搜索内容

Vis*_*sad 1 javascript jquery

我有一个JSON数组 [{"id":38,"label":"def"},{"id":10,"label":"abc"}]

我需要获得结果数组,就像{"id":38,"label":"def"}输入"f"的关键字"d"或"e"一样.

我尝试使用jQuery来做到这一点:

var jsonArrr =[{"id":38,"label":"def"},{"id":10,"label":"abc"}];

var matchMe = new RegExp('^' + 'e', 'i');
    var matches = [];
        for (var i in jsonArrr) {
            if (jsonArrr[i].label.search(matchMe) > -1 ) {
                    matches.push( {'id': i, 'label': jsonArrr[i].label} );
            }
        }

content = '';
for (var i in matches) { 
                console.log(matches[i].label);
                }
Run Code Online (Sandbox Code Playgroud)

但是这段代码输出了

Uncaught TypeError: Cannot read property 'search' of undefined
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Ale*_* T. 6

你可以使用.filterindexOf(在字符串中查找值),就像这样

var data = [{"id":38,"label":"def"}, {"id":10,"label":"abc"}, {"id":38,"label":"ABC"}];

function filter(data, keyword) {
  return data.filter(function (el) {
    return el.label.indexOf(keyword) >= 0;
  })
} 

function filterCaseInsensitive(data, keyword) {
  return data.filter(function (el) {
    return new RegExp(keyword, 'i').test(el.label);
  })
}

$('#keyword').on('keyup', function () {
  $('#result').html( JSON.stringify(filter(data, this.value)) );
  $('#resultCaseInsensitive').html( JSON.stringify(filterCaseInsensitive(data, this.value)) );  
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="keyword" type="text">
<div id="result"></div>
<div id="resultCaseInsensitive"></div>
Run Code Online (Sandbox Code Playgroud)