2016-07-11 60 views
-1

該陣列的用戶關鍵字是輸出:我想要搜索存儲在一個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 「}]

回答

0

下面的代碼應工作

var arr = [{"houseName":"a","houseType":"b","houseFloors":"c","houselocation":"d"},{"houseName":"p","houseType":"q","houseFloors":"r","houselocation":"s"}] 


function queryObject(prop, key) { 
var returnThis; 
for(var i=0, len = arr.length; i< len; i++) { 

if(arr[i][prop] === key) { 
returnThis = arr[i] 
} 
return returnThis 
} 
} 
$('input').on('change', function() { 
var prop = $(this).attr('id'); 
var key = $(this).val(); 
queryObject(prop,key) 
}); 
+0

我嘗試這樣做,但沒有成功。我正在分享一個friddle。可以üPLZ看...... https://jsfiddle.net/gaan10/mvoheo80/ – gaan10

+0

檢查這個 https://jsfiddle.net/kmjm90sd/ – Arif

+0

我想根據用戶輸入搜索。 – gaan10

0

對於一個通用的解決方案,簡單地做一個過濾器在你output陣列

var keyword = "a"; 
var result = output.filter(function(obj){ 
    return Object.keys(obj).filter(function(key){ 
    return obj[ key ].indexOf(keyword) != -1; 
    }).length > 0; 
}); 

//now result has the filtered array 
console.log(result); 

DEMO

var output = [{"houseName":"a","houseType":"b","houseFloors":"c","houselocation":"d"},{"houseName":"p","houseType":"q","houseFloors":"r","houselocation":"s"}]; 
 

 
var keyword = "a"; 
 
var result = output.filter(function(obj){ 
 
    return Object.keys(obj).filter(function(key){ 
 
    return obj[ key ].indexOf(keyword) != -1; 
 
    }).length > 0; 
 
}); 
 

 
//now result has the filtered array 
 
console.log(result);

+0

如果我將一個輸入關鍵字從搜索框中輸入,它將起作用 – gaan10

+0

@ gaan10如果您可以將該文本框的值放入關鍵字中,那麼它將起作用。 – gurvinder372

+0

這是我的修改,我從搜索發送輸入。函數sendToPage(){ var input = document.getElementById(「search」).value; var result = output.filter(function(obj){ return Object.keys(obj).filter(function(key){ return obj [key] .indexOf(input)!= -1; })。長度> 0; }); //現在結果已過濾數組 document.write(result); } – gaan10