问题描述
我有以下 javascript function 来从数组中删除一个项目,但无论我搜索什么,索引搜索总是返回 -1。我是从 jQuery“on”点击函数中调用它的。
function removeElement(array,elem) {
let index = array.indexOf(elem);
if (index > -1) {
array.splice(index,1);
}
}
$('div.test').on('click','a.btn',function(e){
e.preventDefault();
let text = $('span.query').html().trim();
let queries = JSON.parse($('.search').val().trim());
console.log(queries); // This line shows the same results
removeElement(queries,text);
console.log(queries); // as this line
});
其中 $('span.query')
包含文本 def
而 $('.search')
是一个输入文本字段,其中包含一个已被 JSON 字符串化的数组
["abc","def","ghij","klmn"]
无论使用什么来搜索我的查询数组(从上面解析),索引始终为 -1,结果(在元素“删除”尝试之后)始终为
["abc","klmn"]
任何想法这里可能出了什么问题?我一直在为此烦恼。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)