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 call foo(1, 2), foo(1, 3), and foo(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
 |  Show 6 more ments

5 Answers 5

Reset to default 5

My 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