如何检查此 JavaScript 数组中是否存在值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7740241/
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
How to check if value exists in this JavaScript array?
提问by Oseer
I have a JavaScript array, where each new item added to the array gets the next incremental number. An example would be as follows (I hope Im writing this correctly):
我有一个 JavaScript 数组,其中添加到数组中的每个新项目都会获得下一个增量数字。一个例子如下(我希望我写得正确):
ArrayofPeople[0] = [{"id": "529", "name": "Bob"}];
ArrayofPeople[1] = [{"id": "820", "name": "Dave"}];
ArrayofPeople[2] = [{"id": "235", "name": "John"}];
The array is named ArrayofPeople
, storing multiple data points for each person.
该数组名为ArrayofPeople
,为每个人存储多个数据点。
I need to know if an element with id of 820 exists in the array or not. How would this be done?
我需要知道数组中是否存在 id 为 820 的元素。这将如何完成?
采纳答案by Manuel van Rijn
Something like this:
像这样的东西:
function in_array(array, id) {
for(var i=0;i<array.length;i++) {
return (array[i][0].id === id)
}
return false;
}
var result = in_array(ArrayofPeople, 235);
回答by gion_13
You should iterate over the array and manually check if you have a matching id:
您应该遍历数组并手动检查是否有匹配的 id:
function getPersonById(id){
for(var i=0,l=ArrayofPeople.length;i<l;i++)
if(ArrayofPeople[0][i].id == id)
return ArrayofPeople[i];
return null;
}
Of course, this is pretty inefficient. I suggest you store your objects into an associative array (a.k.a. an object) indexed by the person's id. Then, the access to a person with a certain id is immediate since objects are nothing than hash-tables:
当然,这是非常低效的。我建议你将你的对象存储到一个由人的 id 索引的关联数组(也就是一个对象)中。然后,可以立即访问具有特定 id 的人,因为对象只不过是哈希表:
ArrayofPeople = {};
ArrayofPeople[529] = {"id": "529", "name": "Bob"};
ArrayofPeople[820] = {"id": "820", "name": "Dave"};
ArrayofPeople[235] = {"id": "235", "name": "John"};
function getPersonById(id){
return id in ArrayofPeople
? ArrayofPeople[id]
: null;
}
回答by Ja?ck
You can use the relatively new Array.prototype.some()
to find whether an item exists (a shim is provided in the documentation):
您可以使用相对较新的Array.prototype.some()
来查找项目是否存在(文档中提供了垫片):
var ArrayofPeople = [];
ArrayofPeople[0] = [{"id": "529", "name": "Bob"}];
ArrayofPeople[1] = [{"id": "820", "name": "Dave"}];
ArrayofPeople[2] = [{"id": "235", "name": "John"}];
function in_array(array, id)
{
return array.some(function(item) {
return item[0].id === id;
});
}
console.log(in_array(ArrayofPeople, '820')); // true
回答by Ehasanul Hoque
function IsIdInArray(array, id) {
for (var i = 0; i < array.length; i++) {
if (array[i].id === id)
return true;
}
return false;
}
var result = IsIdInArray(ArrayofPeople, 820);
回答by Riz
ArrayofPeople = new Array();
ArrayofPeople[0] = [{"id": "529", "name": "Bob"}];
ArrayofPeople[1] = [{"id": "820", "name": "Dave"}];
ArrayofPeople[2] = [{"id": "235", "name": "John"}];
var str = '820';
var is_found = 'not found';
for(item in ArrayofPeople){
target = ArrayofPeople[item][0];
if(target['id'] === str)
is_found = 'found';
}
alert(is_found);