我想从javascript中的json对象中搜索一个字符串

我想从javascript中的json对象中搜索一个字符串,javascript,string,object,search,Javascript,String,Object,Search,我想从用户提供的所有输入中搜索房屋名称。 因此,如果用户详细信息如下所示: [{“houseName”:“man”,“houseType”:“villa”,“houseFloors”:“seven”,“houselocation”:“Seattle”},{“houseName”:“band”,“houseType”:“small”,“houseFloors”:“two”,“houselocation”:“washington DC”}] 如果我以人的身份提供搜索,它应该给我以下信息: [{“房屋

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

[{“houseName”:“man”,“houseType”:“villa”,“houseFloors”:“seven”,“houselocation”:“Seattle”},{“houseName”:“band”,“houseType”:“small”,“houseFloors”:“two”,“houselocation”:“washington DC”}]

如果我以人的身份提供搜索,它应该给我以下信息:

[{“房屋名称”:“人”,“房屋类型”:“别墅”,“房屋楼层”:“七”,“房屋位置”:“西雅图”}]

守则如下:

<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
门名


户型

房屋楼层:

房屋位置:

搜索: 添加详细信息 显示 变量列表=[], $ins=$(“#房屋名称,#房屋类型,#房屋楼层,#房屋位置”), 变量计数器={ 房屋名称:{}, 房屋类型:{}, 楼层:{}, 房屋位置:{} }; $('#添加')。单击(函数(){ var obj={}, 有效=真; $ins.each(函数(){ var val=该值; if(val){ obj[this.id]=val; }否则{ 警报(“不能为空”); 返回false; } }); 如果(有效){ 列表推送(obj); $ins.val(“”); } }); $(“#打印”)。单击(函数(){ $('pre').text(JSON.stringify(list)+'\n\n'); }) var关键字=$(“#搜索输入”).val(); var filteredList=list.filter(函数(用户){ return user.houseName==='man';//或者如果需要检查关键字是否包含,可以使用indexOf });
您可以使用
数组.prototype.filter

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

//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
如果您想使用输入框进行搜索,还需要做一些工作:


您可以使用
Array.prototype.filter

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

//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
如果您想使用输入框进行搜索,还需要做一些工作:


如果我创建了一个搜索框字段,并且它搜索的是人,那么它应该是什么样子呢?@gaan10–投稿人不是来填鸭式喂养的。。。。对提供的解决方案做一些研究。。还请注意,提供的代码包含错误…上述代码不起作用。我创建了一个搜索框,并试图从中提取关键字。如果我创建了一个搜索框字段,并且它搜索man,那么它应该是什么样子。@gaan10–投稿人不是来填鸭式喂养的。。。。对提供的解决方案做一些研究。。还请注意,提供的代码包含错误…上述代码不起作用。我创建了一个搜索框,并试图从中获取关键字。