英文:
JavaScript array filter not working as expected
问题
我有一个类似这样的数组:
<!-- 开始代码段: js 隐藏: false 控制台: true babel: false -->
<!-- 语言: lang-js -->
var arrayd = [{
"CATEGORY_ID": "1",
"CATEGORY": "a",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "b",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "c",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "d",
},
{
"CATEGORY_ID": "3",
"CATEGORY": "e",
},
{
"CATEGORY_ID": "18",
"CATEGORY": "f",
}
];
var searchTerm = 1;
arrayd = arrayd.filter(
s => parseInt(s.CATEGORY_ID.indexOf(parseInt(searchTerm))) !== -1
);
console.log(arrayd);
<!-- 结束代码段 -->
我试图使用 `searchTerm` 来过滤这个数组。
在这种情况下,`searchTerm` 的值为 1,但是响应中元素的 ID 为 `18` 也被返回了。
如何修复这个问题?
注意:`searchTerm` 的值始终为数字。
[Fiddle][1]
[1]: https://jsfiddle.net/67qkf4vx/
英文:
I have an array like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var arrayd = [{
"CATEGORY_ID": "1",
"CATEGORY": "a",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "b",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "c",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "d",
},
{
"CATEGORY_ID": "3",
"CATEGORY": "e",
},
{
"CATEGORY_ID": "18",
"CATEGORY": "f",
}
];
var searchTerm = 1;
arrayd = arrayd.filter(
s => parseInt(s.CATEGORY_ID.indexOf(parseInt(searchTerm))) !== -1
);
console.log(arrayd);
<!-- end snippet -->
What I'm trying to dimis filter this array using a searchTerm
.
In this scenario, searchTerm
has value 1 but in the response, element with id 18
is also getting returned.
How to fix this?
Note: searchTerm
will always be numbers.
答案1
得分: 3
问题在于18
如果你将其视为字符串
,则包含了1
。正如String.prototype.indexOf()
文档所述:
> indexOf()方法返回指定值在调用的字符串对象中的第一个出现的索引,从fromIndex开始搜索。如果未找到该值,则返回-1。
因此,indexOf
返回了一个不是-1
的值,这就是为什么你也从数组中获取到了该对象。
只需简单地使用===
和parseInt
如下所示:
var arrayd = [{
"CATEGORY_ID": "1",
"CATEGORY": "a",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "b",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "c",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "d",
},
{
"CATEGORY_ID": "3",
"CATEGORY": "e",
},
{
"CATEGORY_ID": "18",
"CATEGORY": "f",
}
];
var searchTerm = 1;
arrayd = arrayd.filter(
s => parseInt(s.CATEGORY_ID) === searchTerm
);
console.log(arrayd);
希望这有所帮助!
英文:
The issue is that 18
is containing 1
if you consider as a string
. As String.prototype.indexOf()
documentation states:
> The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.
So indexOf
returned a not -1
value and that's why you got back that object as well from your array.
Just simply use ===
and parseInt
as follows:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var arrayd = [{
"CATEGORY_ID": "1",
"CATEGORY": "a",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "b",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "c",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "d",
},
{
"CATEGORY_ID": "3",
"CATEGORY": "e",
},
{
"CATEGORY_ID": "18",
"CATEGORY": "f",
}
];
var searchTerm = 1;
arrayd = arrayd.filter(
s => parseInt(s.CATEGORY_ID) === searchTerm
);
console.log(arrayd);
<!-- end snippet -->
I hope this helps!
答案2
得分: 0
将CATEGORY
转换为数字并检查是否等于搜索词:
const arrayd = [{"CATEGORY_ID":"1","CATEGORY":"a"},{"CATEGORY_ID":"1","CATEGORY":"b"},{"CATEGORY_ID":"1","CATEGORY":"c"},{"CATEGORY_ID":"1","CATEGORY":"d"},{"CATEGORY_ID":"3","CATEGORY":"e"},{"CATEGORY_ID":"18","CATEGORY":"f"}];
const searchTerm = 1;
const result = arrayd.filter(
s => +s.CATEGORY_ID === searchTerm
);
console.log(result);
英文:
Convert the CATEGORY
to a number and check if it's equal to the search term:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arrayd = [{"CATEGORY_ID":"1","CATEGORY":"a"},{"CATEGORY_ID":"1","CATEGORY":"b"},{"CATEGORY_ID":"1","CATEGORY":"c"},{"CATEGORY_ID":"1","CATEGORY":"d"},{"CATEGORY_ID":"3","CATEGORY":"e"},{"CATEGORY_ID":"18","CATEGORY":"f"}];
const searchTerm = 1;
const result = arrayd.filter(
s => +s.CATEGORY_ID === searchTerm
);
console.log(result);
<!-- end snippet -->
答案3
得分: 0
我不知道为什么你要以如此复杂的方式去做这件事,但你可以轻松地通过以下方式实现你想要的效果:
arrayd = arrayd.filter(
s => parseInt(s.CATEGORY_ID)===searchTerm
);
这将返回你期望的4个结果。
英文:
I don't know why you're trying to do that in such a convoluted way but you can easily achieve what you want by using:
arrayd = arrayd.filter(
s => parseInt(s.CATEGORY_ID)===searchTerm
);
This will return the 4 results you expect.
答案4
得分: 0
我希望以下答案能帮助您。这是一个用于检查searchItem的回调通用函数。
var arrayd = [{
"CATEGORY_ID": "1",
"CATEGORY": "a",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "b",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "c",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "d",
},
{
"CATEGORY_ID": "3",
"CATEGORY": "e",
},
{
"CATEGORY_ID": "18",
"CATEGORY": "f",
}
];
var searchTerm = 1;
arrayd = arrayd.filter(s => parseInt(s.CATEGORY_ID) == searchTerm)
console.log(arrayd);
您可以在以下链接中查看完整的代码:http://jsfiddle.net/7mx8yj3z/
英文:
I hope the following answer will help you. It's a callback common function to check with the searchItem.
var arrayd = [{
"CATEGORY_ID": "1",
"CATEGORY": "a",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "b",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "c",
},
{
"CATEGORY_ID": "1",
"CATEGORY": "d",
},
{
"CATEGORY_ID": "3",
"CATEGORY": "e",
},
{
"CATEGORY_ID": "18",
"CATEGORY": "f",
}
];
var searchTerm = 1;
arrayd = arrayd.filter(s => parseInt(s.CATEGORY_ID) == searchTerm)
console.log(arrayd);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论