javascript 迭代javascript中的一组对象并从该函数返回true
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15282480/
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
Iterate over a collection of objects in javascript and return true from that function
提问by JZ.
I have an object called collection, and I want to test to see if justin is part of this collection.
我有一个名为 collection 的对象,我想测试一下 justin 是否属于这个集合。
collection = { 0:{screen_name:"justin"},1:{screen_name:"barry"}}
I'm trying to discover the most efficient method, to pass in a name to function called present_user(user)
, to see if the user is part of the collection and I'm kind of stumped.
我试图发现最有效的方法,将名称传递给调用的函数present_user(user)
,看看用户是否是集合的一部分,我有点难住了。
So my collection is built up of objects 0, 1, n+1. I'm trying to iterate through this collection. So far I only test [0]
所以我的收藏是由对象 0、1、n+1 组成的。我正在尝试遍历这个集合。到目前为止我只测试 [0]
function present_user(user) {
collection[0]["screen_name"] == user -> return true in the case of "justin"
}
How can I iterate over all values of this collection, and return true if the user_name "justin" is passed into a function?
如何遍历此集合的所有值,并在将 user_name "justin" 传递给函数时返回 true?
回答by mVChr
Your collection is an object and not an array, so this would be a way to do it:
你的集合是一个对象而不是一个数组,所以这将是一种方法:
var present_user = function(user){
for (var k in collection) {
if (collection[k]['screen_name'] == user) return true;
}
return false;
};
回答by bfavaretto
If your outer object keys are all numbers, you should be using an array instead:
如果您的外部对象键都是数字,则应该使用数组:
var collection = [{screen_name:"justin"}, {screen_name:"barry"}];
Then iterate with:
然后迭代:
function present_user(user) {
for(var i=0; i < collection.length; i++) {
if(collection[i].screen_name === user) return true;
}
}
You could loop the object collection too (with for..in
, see mVChr's answer), but in this case it looks like you really should be using an array.
您也可以循环对象集合(使用for..in
,请参阅mVChr 的回答),但在这种情况下,您似乎确实应该使用数组。
回答by joronimo
You could also turn a collection or object into an array, so that you could use array methods to iterate:
您还可以将集合或对象转换为数组,以便可以使用数组方法进行迭代:
Object.values(collectionOrObject);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values
This especially comes in handy when you use the native javascript dom selectors like:
当您使用本机 javascript dom 选择器时,这尤其有用,例如:
document.getElementsByClassName('someClass');
Which returns a collection
返回一个集合
回答by Explosion Pills
If you know how many objects are in the collection ahead of time, you can use add a length
property and use
如果您提前知道集合中有多少对象,则可以使用添加length
属性并使用
Array.prototype.some.call(collection, function (elem) {
return elem.screen_name = 'justin';
});
Ideally collection
would be an array initially so you could just use collection.some
, but I understand that may not be possible.
理想情况下collection
最初是一个数组,因此您可以只使用collection.some
,但我知道这可能是不可能的。
If you have no way of knowing the length ahead of time in that case, you have to iterate manually. Sorry.
如果在这种情况下您无法提前知道长度,则必须手动迭代。对不起。
var exists = false, i;
for (i in collection) {
if (collection.hasOwnProperty(i)) {
/* may also want to check `screen_name` property existence
if (collection[i].screen_name == 'justin') {
exists = true;
break;
}
}
}
回答by TKharaishvili
function any(user) {
return collection.filter(function (item) { return item == user }).length > 0;
}
The only "gotcha" here is that you need to have an array here not an object, if you have no problem modifying the collection variable to array, this functional approach would be the most elegant one, in my opinion.
这里唯一的“问题”是您需要在此处使用数组而不是对象,如果将集合变量修改为数组没有问题,在我看来,这种函数式方法将是最优雅的方法。
One more thing to take under consideration though is that this code will not stop the moment it finds the first match, it will test all the items in array instead so I would recommend using it only if the array is not large.
但要考虑的另一件事是,此代码不会在找到第一个匹配项的那一刻停止,而是会测试数组中的所有项目,因此我建议仅在数组不大时才使用它。