【问题标题】:i want to search a string from the json object in javascript我想从 javascript 中的 json 对象中搜索一个字符串
【发布时间】:2016-11-11 19:14:03
【问题描述】:

我想从用户提供的所有输入中搜索房屋名称。 所以如果用户详细信息如下:

[{"houseName":"man","houseType":"villa","houseFloors":"seven","houselocation":"Seattle"},{"houseName":"band","houseType" :"small","houseFloors":"two","houselocation":"华盛顿特区"}]

如果我以 man 身份提供搜索,它应该给我:

[{"houseName":"man","houseType":"villa","houseFloors":"seven","houselocation":"Seattle"}]

代码如下:

<html>
<head>
<title>JavaScript</title>
</head>

<body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label>House Name
  <input type='text' name='houseName' id='houseName' placeholder="House Name">
</label>
<br>
<br>
<label>House type
  <input type='text' name='houseType' id='houseType' placeholder="House type">
</label>
<br>
<br>
<label>House Floors:
  <input type='text' name='houseFloors' id='houseFloors' placeholder="House Floors">
</label>
<br>
<br>
<label>House Location:
  <input type='text' name='houselocation' id='houselocation' placeholder="House Location">
</label>
<br>
<br>
<div>
<label>search:
  <input type="text" name="search" id="search-input" placeholder="search">
  <input type="submit">
</div>
<button type="button" id="add">Add Details</button>
<button type="button" id="print">Show</button>

<pre></pre>
<script>
    var list = [],
  $ins = $('#houseName, #houseType, #houseFloors, #houselocation'),
  var counter = {
    houseName: {},
    houseType: {},
    houseFloors: {},
    houselocation: {}
  };

 $('#add').click(function() {
  var obj = {},
    valid = true;
  $ins.each(function() {
    var val = this.value;
    if (val) {
      obj[this.id] = val;
    } else {

      alert(" Cannot be blank");

      return false;
    }
  });
  if (valid) {
    list.push(obj);
    $ins.val('');

  }
});

$('#print').click(function() {
  $('pre').text(JSON.stringify(list) + '\n\n');

})
var keyword = $('#search-input').val();
var filteredList = list.filter(function(user){
   return user.houseName === 'man'; // Or u can use indexOf if u want check if the keyword is contained
});


</script>

</body>

</html>

【问题讨论】:

    标签: javascript string object search


    【解决方案1】:

    您可以使用Array.prototype.filter

    在你的情况下,它看起来像

    var filteredList = list.filter(function(user){
       return user.houseName === 'man'; // Or u can use indexOf if u want check if the keyword is contained
    });
    

    如果你想用输入框搜索它,还有一点工作要做:

    //The follow code should be executed when u are going to do the 'search' action
    //It could be an click on a button, or just in a listener which is triggered when the search box fires 'change' events
    
    //First u need to get the keyword from the search input box:
    var keyword = $('#search-input').val();
    
    //maybe do some value check
    //if (keyword === '') return;
    
    //then get the filtered List
    var filteredList = list.filter(function(user){
       return user.houseName === keyword; 
    });
    
    //Finally update the UI based on the filtered List
    //Maybe jQuery's DOM functions could be helpful
    

    【讨论】:

    • 如果我创建一个搜索框字段并且如果它搜索 man 那么它应该是什么样子。
    • @gaan10 - 贡献者不是来喂饭的......对提供的解决方案进行一些研究......还要注意提供的代码包含错误......
    • 上面的代码不起作用。我创建了一个搜索框并尝试从那里获取关键字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多