該陣列的用戶關鍵字是輸出:我想要搜索存儲在一個JSON對象,在JavaScript
[{"houseName":"a","houseType":"b","houseFloors":"c","houselocation":"d"},{"houseName":"p","houseType":"q","houseFloors":"r","houselocation":"s"}]
我的代碼:
<html lang>
<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>
<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');
})
</script>
</body>
</html>
現在我想的是當用戶提供了一個關鍵字,我想通過這個輸出在javascript中搜索該特定的關鍵字,並返回該特定對象的完整細節。
例如:如果關鍵字是:一個 輸出應爲: [{ 「houseName」: 「一」, 「houseType」: 「B」, 「houseFloors」: 「C」, 「houselocation」:「d 「}]
我嘗試這樣做,但沒有成功。我正在分享一個friddle。可以üPLZ看...... https://jsfiddle.net/gaan10/mvoheo80/ – gaan10
檢查這個 https://jsfiddle.net/kmjm90sd/ – Arif
我想根據用戶輸入搜索。 – gaan10