Javascript 在数组或类似的地方使用 jQuery.find()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12748203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using jQuery.find() on array or similar
提问by K20GH
Lets say I have a search box and whatever is typed in it is stored as:
假设我有一个搜索框,其中输入的任何内容都存储为:
var search_input = $(this).val();
How would I search my array with this value?
我将如何使用此值搜索我的数组?
var devices = [{"Device_ID":"43","Image":"Amazon-Kindle-Fire.png","Manufacturer":"Amazon","Model":"Kindle Fire","Type":"Tablet"},{"Device_ID":"44","Image":"Apple-iPad.png","Manufacturer":"Apple","Model":"iPad","Type":"Tablet"}]
So lets say I had Amazon, or Kindle etc in my search_input variable, I would want it to search the array and output the below. The same would apply for any thing typed or anything in the array
因此,假设我的 search_input 变量中有 Amazon 或 Kindle 等,我希望它搜索数组并输出以下内容。这同样适用于输入的任何内容或数组中的任何内容
<div id="device">43</div>
<div id="image">Amazon-Kindle-Fire.png</div>
<div id="name">Amazon Kindle Fire</div>
回答by epascarello
Simple loop in a loop
循环中的简单循环
var devices = [{"Device_ID":"43","Image":"Amazon-Kindle-Fire.png","Manufacturer":"Amazon","Model":"Kindle Fire","Type":"Tablet"},{"Device_ID":"44","Image":"Apple-iPad.png","Manufacturer":"Apple","Model":"iPad","Type":"Tablet"}];
function findItem (term) {
var items = [];
for (var i=0;i<devices.length;i++) {
var item = devices[i];
for (var prop in item) {
var detail = item[prop].toString().toLowerCase();
if (detail.indexOf(term)>-1) {
items.push(item);
break;
}
}
}
return items;
}
console.log( findItem("amazon") ); //returns one item
console.log( findItem("kindle") ); //returns one item
console.log( findItem("apple") ); //returns one item
console.log( findItem("tablet") ); //returns two items
console.log( findItem("nerd") ); //returns zero items
?
回答by Daniel
I think a more elegant way to achieve the goal in this question would be to use jQuery.grep()
--
see http://api.jquery.com/jquery.grep/
我认为在这个问题中实现目标的更优雅的方法是使用jQuery.grep()
- 请参阅http://api.jquery.com/jquery.grep/
var search_input = $(this).val();
var devices = [
{"Device_ID":"43","Image":"Amazon-Kindle-Fire.png","Manufacturer":"Amazon","Model":"Kindle Fire","Type":"Tablet"},
{"Device_ID":"44","Image":"Apple-iPad.png","Manufacturer":"Apple","Model":"iPad","Type":"Tablet"}
];
var matchingItems = $.grep(devices, function(e) {
return (e["Manufacturer"].indexOf(search_input) >- 1 ||
e["Model"].indexOf(search_input) > -1);
});
Now matchingItems
contains an array of items that have search_input word in Manufacturer or Model properties, and you can output those in html.
现在matchingItems
包含在制造商或型号属性中具有 search_input 词的项目数组,您可以在 html 中输出这些项目。
回答by Carlos Martinez T
回答by Johanna Larsson
Another way would be to replace your array with a map (JavaScript object) where the key is the value you want to compare with the search_input. That way you simply look up the key and get the object.
另一种方法是用映射(JavaScript 对象)替换数组,其中键是要与 search_input 进行比较的值。这样,您只需查找密钥并获取对象。
var devices = {"Amazon Kindle": {"Device_ID":"43","Image":"Amazon-Kindle-Fire.png","Manufacturer":"Amazon","Model":"Kindle Fire","Type":"Tablet"},{"Device_ID":"44","Image":"Apple-iPad.png","Manufacturer":"Apple","Model":"iPad","Type":"Tablet"}}
The second step involves converting the JavaScript object to XML, which is already answered on SO here What's the best way to serialize JavaScript objects to XML?
第二步涉及将 JavaScript 对象转换为 XML,这里已经在 SO 上回答了将 JavaScript 对象序列化为 XML 的最佳方法是什么?