admin管理员组文章数量:1293159
I currently have an array data structure that I iterate over like this, calling foo
on each unique pair of elements.
for(var i = 0; i < arr.length; i++) {
for(var j = i + 1; j < arr.length; j++) {
foo(arr[i], arr[j]);
}
}
However, I've realized that I'd rather use an object instead of an array, since I can then add and remove elements by name very easily.
However, I can't see an obvious way to iterate over such an object. The closest I can get is:
for(i in obj) {
for(j in obj) {
foo(obj[i], obj[j]);
}
}
Obviously, this will do each pair twice, and even produce a pair of identical elements. Is there an easy way to iterate over an object in the same way as I do in the array in my first code sample?
Update:
Performance testing the solutions on jsperf.
I currently have an array data structure that I iterate over like this, calling foo
on each unique pair of elements.
for(var i = 0; i < arr.length; i++) {
for(var j = i + 1; j < arr.length; j++) {
foo(arr[i], arr[j]);
}
}
However, I've realized that I'd rather use an object instead of an array, since I can then add and remove elements by name very easily.
However, I can't see an obvious way to iterate over such an object. The closest I can get is:
for(i in obj) {
for(j in obj) {
foo(obj[i], obj[j]);
}
}
Obviously, this will do each pair twice, and even produce a pair of identical elements. Is there an easy way to iterate over an object in the same way as I do in the array in my first code sample?
Update:
Performance testing the solutions on jsperf.
Share edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked May 8, 2012 at 19:37 EricEric 97.7k54 gold badges255 silver badges389 bronze badges 11-
1
How about adding an
if (i < j)
condition in the inner loop? It's not the fastest solution, but I believe it could work. – Simon Forsberg Commented May 8, 2012 at 19:43 -
2
@elclanrs: I'm not iterating recursively - I'm iterating over every possible bination of entries in an object/array. Eg, in the array
[1, 2, 3]
, I want to callfoo(1, 2)
,foo(1, 3)
, andfoo(2, 3)
. – Eric Commented May 8, 2012 at 19:43 - 1 @SimonAndréForsberg that won't work in each case because the keys of an object don't have to be sorted and they don't have to be of the same data type as well. – Johannes Egger Commented May 8, 2012 at 19:46
- 1 @Eric you are right. But strings (object keys) are pared differently to integers (array keys) (e.g. "10" < "2" and 10 < 2 leads to a different result). – Johannes Egger Commented May 8, 2012 at 19:50
-
1
@Jasd: Nope, array keys are strings as well. Try doing a
for ... in
loop with an array. The keys you get are not integers, but strings. – Eric Commented May 8, 2012 at 20:00
5 Answers
Reset to default 5My solution that was at first written as a ment:
Add an if (i < j)
condition in the inner loop. It might not be the best solution, but it would work as long as the foo function does the same thing for foo(2, 10)
and foo(10, 2)
:
for(i in obj) {
for(j in obj) {
if (i < j) {
foo(obj[i], obj[j]);
}
}
}
Assuming I understand your question... maybe check to see if the value has already been visited by the outer loop?
var visited = {}
for(i in obj) {
visited[i] = true;
for(j in obj) {
if(j in visited){ continue; }
foo(obj[i], obj[j]);
}
}
Use Object.keys() to get the list of keys out as an array:
keys = Object.keys();
for(i=0;i<keys.length;i++) {
for(j=i+1;j<keys.length;j++) {
foo(obj[keys[i]], obj[keys[j]]);
}
}
Maybe You can try unset used objects:
for(i in obj) {
var a = obj[i];
delete obj[i];
for(j in obj) {
foo(a, obj[j]);
}
}
http://jsfiddle/bXcvb/
If you need to original obj in tact see: How do I correctly clone a JavaScript object?
You can push the object keys into an array:
var obj_keys = [];
for (i in obj) {
obj_keys.push(i);
}
for(i = 0; i < obj_keys.length; ++i) {
for(j = i + 1; j < obj_keys.length; ++j) {
foo(obj[obj_keys[i]], obj[obj_keys[j]]);
}
}
本文标签: javascriptHow can I iterate over all unique pairs of entries in an objectStack Overflow
版权声明:本文标题:javascript - How can I iterate over all unique pairs of entries in an object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741573933a2386171.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论