Javascript 检查文本是否属于jquery中列表中的哪个类别?
我正在使用一个类别数组,即Javascript 检查文本是否属于jquery中列表中的哪个类别?,javascript,jquery,asp.net,list,search,Javascript,Jquery,Asp.net,List,Search,我正在使用一个类别数组,即CategoryListSearch,其中包含各种类别,如移动电子产品、珠宝等 我在文本框中使用了一个文本框,我插入了一个类似IPhone的值,因此如何从列表中匹配我的手机属于手机类别。 我用这个作为样本 if (txt != '') { $.each(CategoryListSearch, function(i, o) { }); 但不知道如何比较CategoryListSearch中的文本值 请帮忙 试试这个 if(CategoryListSearch.ind
CategoryListSearch
,其中包含各种类别,如移动电子产品、珠宝等
我在文本框中使用了一个文本框,我插入了一个类似IPhone的值,因此如何从列表中匹配我的手机属于手机类别。
我用这个作为样本
if (txt != '') {
$.each(CategoryListSearch, function(i, o) {
});
但不知道如何比较CategoryListSearch
中的文本值
请帮忙 试试这个
if(CategoryListSearch.indexOf(text) > -1) {
//text is present
}
像这样。在循环内部使用三元运算符
(?:)
if (txt != '') {
$.each(CategoryListSearch, function(i, o) {
var present = (o == txt) ? "Item present in list" : "No item in list";
console.log(present);
}
});
假设您的对象是树状结构,下面的函数首先检查我们是否正在搜索对象本身。如果不是,则迭代它并搜索索引/成员
getCategoryPath(obj, item) {
if (obj === item) {
return obj;
}
for (index in obj) {
if ((typeof obj[index] === "undefined") || (obj[index] === null)) {
//irrelevant leaf
} else if (typeof obj[index] === "object") {
var ret = getCategoryPath(obj[index], item);
if (ret) {
return ret.unshift(index);
}
} else if ((obj[index] === item) || (index === item)) {
return [index];
}
}
}
有了将近300个rep,您应该知道如何单击
并创建一个-什么是CategoryListSearch?CategoryListSearch是数组还是对象?我们需要这个神秘变量内容的例子。你能给我们看看CategoryListSearch的片段吗?